1

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.

1 Answer 1

2

Without seeing the full content of those actions, it's hard to say, but I'd guess based upon that error message you have return View(); near the bottom of both of your post actions. Most likely your code inside of both of the posts is actually running, it just doesn't know what to do at the end.

You have several options.

  1. Create views for those actions with the same name
  2. Modify the return View(); to something like return View("Admin"); so it returns the admin view
  3. Change it to a redirect...ex. return RedirectToAction("controller","view");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.