M.Peter
M.Peter

Reputation: 85

How to submit form in MVC

I have code on views bellow:

@using (@Html.BeginForm("DeleteItem", "Test", new { id = 3 }, FormMethod.Post, new { @class = "form" }))
{
  @Html.AntiForgeryToken()
  <a class="submit-process" href="javascript:void(0);"><i class="fa fa-trash-o"></i> Delete</a>
}

Script code:

$('.submit-process').click(function () {
    if (confirm('You want delete?')) {
        $(this).closest("form").submit();
    }
    else return false;
});

And Action in Controlller Test bellow:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteItem(int id)
    {
        return View();
    }

When I click submit, It not found action DeleteItem, and message error:

The view 'DeleteItem' or its master was not found or no view engine supports the searched locations

Upvotes: 2

Views: 538

Answers (1)

Smit Patel
Smit Patel

Reputation: 3247

This thing is occurred because you have not specified your viewName in

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DeleteItem(int id)
{
    return view('YOUR VIEW NAME');
}

And your example this type of error may occurred.

The view 'DeleteItem' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Test/DeleteItem.aspx
~/Views/Test/DeleteItem.ascx
~/Views/Shared/DeleteItem.aspx
~/Views/Shared/DeleteItem.ascx
~/Views/Test/DeleteItem.cshtml
~/Views/Test/DeleteItem.vbhtml
~/Views/Shared/DeleteItem.cshtml
~/Views/Shared/DeleteItem.vbhtml

When you don't set a viewname in return view(), It's automatically takes the Method name as a viewname and try to find it in above locations.

Look more Here

Learn more about Controllers and Action Methods in ASP.NET MVC Applications.

Upvotes: 4

Related Questions