Home > Mobile >  Delete post method returns null
Delete post method returns null

Time:02-09

I am following a beginner tutorial on how to make a simple store with asp.net mvc, and in tutorial there is no problem when doing exact same steps. I am currently trying to perform a basic CRUD operations on my category page, but i am stuck when trying to delete categories. I get not found page because id is null, but i don't have problem for Edit method when passing the same id parameter. I was looking for an answer and some people suggest that there might be caching problem, but not sure how to even try to fix that. Here is my controller for delete operations

 // GET-DELETE
        public IActionResult Delete(int? id)
        {
            
            if (id == null || id == 0)
            {
                return NotFound();
            }

            Category obj = _db.Category.Find(id);

         if (obj == null)
            {
                return NotFound();
            }

            return View(obj);
        }

        //POST-DELETE
        [HttpPost]
        [ValidateAntiForgeryToken]

        public IActionResult DeletePost(int? id)
        {
            Category obj = _db.Category.Find(id);

            if (id == null)
            {
                return NotFound();
            }
            
            
            _db.Category.Remove(obj);
            _db.SaveChanges();
            return RedirectToAction("Index");
           
           
        }

and here is my View

@model RockyWebsite.Models.Category

<form method="post" asp-action="DeletePost">
    @Html.HiddenFor(id => id.CategoryId)
    <input asp-for="CategoryId" hidden />

    <div >

        <div >
            <h2 >Delete Category</h2>
        </div>
        <div >
            <div >
                <div >
                    <div >
                        <label asp-for="CategoryName"></label>
                    </div>
                    <div >
                        <input asp-for="CategoryName" disabled  />

                    </div>

                </div>
                <div >
                    <div >
                        <label asp-for="DisplayOrder"></label>
                    </div>
                    <div >
                        <input asp-for="DisplayOrder" disabled  />

                    </div>

                </div>
                <div >
                    <div >

                        <div >
                            <input type="submit"  value="Delete" />
                        </div>
                        <div >
                            <a asp-action="Index" ><i ></i> Back</a>
                        </div>
                    </div>
                </div>
            </div>
            <div >
                @* Keep this empty *@
            </div>
        </div>
    </div>

</form>

Any help or suggestion would be very appreciated, thanks!

CodePudding user response:

add name tag to input, for example :

<input name="id" asp-for="CategoryId" hidden />

Alex

CodePudding user response:

You're using @Html.HiddenFor(id => id.CategoryId) (well, you're actually using the tag-helper syntax too <input asp-for="CategoryId" hidden /> and you should just use one or the other, not both!) in the view which will create an input with name="CategoryId".

So, the easiest solution is probably to correct the view and update the parameter name in the controller action for DeletePost.

View:

<!-- remove this line: @Html.HiddenFor(id => id.CategoryId) -->
<!-- just use line below -->
<input type="hidden" asp-for="CategoryId" />

Controller:

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult DeletePost(int? categoryId)
{
    Category obj = _db.Category.Find(categoryId);
    // check obj here, not id
    if (obj == null)
    {
        return NotFound();
    }    
    
    _db.Category.Remove(obj);
    _db.SaveChanges();
    return RedirectToAction("Index");
}
  •  Tags:  
  • Related