I have a MVC project I inherited. On the views, there's no route (controller and action) specified in the BeginForm tag. The view renders correctly so I assume it is picking a default route.
My question is how does it know what route to use if one isn't specified? What's the best practice here: should you specify a route or let it default?
So the view is Views/Config/WorkCodes.cshtml and the tag is
Html.BeginForm()
It goes to the controller ConfigController.cs and calls action WorkCodes(). If I was doing the project, I would have wrote
Html.BeginForm("WorkCodes", "Config", FormMethod.Post)
How does MVC know which controller and action to use without specifying it?
WorkCodes.cshtml
@{
ViewBag.Title = "Work Codes";
}
@using (Html.BeginForm())
{
<div>
<table style="width: 100%;" class="trHoverHighlight">
<tbody>
<tr>
<td>
<br />
<div>
<button id="buttonCreateNew" type="button">Add New</button>
<button id="buttonReturn" type="button">Return</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
}
ConfigController.cs
public ActionResult WorkCodes()
{
return View(Rep.GetWorkAll(true));
}
Here's where the view is called from in another view:
@foreach (var itm in (List<string>)ViewBag.ListObjects)
{
<li>
<a href="../Config/@itm">Work Codes</a>
</li>
}
The View is generated from performing
HTTP GET
to theWorkCodes
controller action, thus by default the form generated in that view performs anHTTP POST
to a controller action with the same name.Here's the MSDN docs.