First of all I want to ask for my bad English. I'm very new to ASP.NET MVC and currently writing my first web app.
I have a controller like this:
namespace MaterialProject.Controllers
{
public class AdminController : Controller
{
[HttpGet]
public IActionResult Admin()
{
//file some Viewbags from database
}
[HttpPost("UpdatePassword")]
[ValidateAntiForgeryToken]
public IActionResult UpdatePassword(EditUser euModel)
{
//database update
}
[HttpPost("UpdateGroupID")]
[ValidateAntiForgeryToken]
public IActionResult UpdateGroupID(EditUser euModel)
{
//database update
}
}
}
and I want my view to have 2 submit buttons when the user will choose UpdatePassword I want to execute that action and the user choose UpdateGroupID to execute the other action.
My view called Admin.chtml
<div class="container">
<form asp-controller="Admin" asp-action="UpdatePassword" method="post" class="form-horizontal" role="form">
<div class="alert-danger" asp-validation-summary="ModelOnly"></div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
<label asp-for="Username" class="control-label" value=""></label>
<select asp-for="ID" class="form-control"
asp-items="@(new SelectList(ViewBag.editUser, "ID", "Username"))"></select>
</div>
<div id="submit">
<input type="submit" name="submit" value="Update Password" />
</div>
</form>
<form asp-controller="Admin" asp-action="UpdateGroupID" method="post" class="form-horizontal" role="form">
<div class="alert-danger" asp-validation-summary="ModelOnly"></div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
<label asp-for="Username" class="control-label" value=""></label>
<select asp-for="ID" class="form-control"
asp-items="@(new SelectList(ViewBag.editUser, "ID", "Username"))"></select>
</div>
<div id="submit">
<input type="submit" name="submit" value="Update GroupID" />
</div>
</form>
</div>
If I execute the above code I get this error:
An unhandled exception occurred while processing the request.
InvalidOperationException: The view 'UpdatePassword' was not found. The following locations were searched:
/Views/Admin/UpdatePassword.cshtml /Views/Shared/UpdatePassword.cshtml /Pages/Shared/UpdatePassword.cshtml
Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable originalLocations)
Because I have never created that view.
Is there any way by pressing any from the submit buttons on my form to call different blocks of code from my controller?
Thanks in advance for your help.