Skip to content Skip to sidebar Skip to footer

Submit Form In Mvc Using Html

I have web application that is using MVC5 with C#. In this app, I have a form with one dropdownlist and a submit button that looks like the following:

View:

<form class="form-horizontal" action="../TemplateMapper/AssignTemplate" method="post">
    <divclass="control-group"><labelclass="control-label">Template:</label><divclass="controls"><selectid="template"name="myDropDownList">
                @foreach (KeyValuePair<int, string> entry in Model.templates)
                {
                    <optionvalue="@entry.Key">@entry.Value</option>
                }
            </select></div></div><divclass="control-group"><divclass="controls"><inputtype="submit"class="btn btn-primary"value="Assign" /></div></div></form>

Controller

public ActionResult AssignTemplate(string myDropDownList)
{
     return View();
}

Assuming you have a View named AssignTemplate

Solution 2:

The button that you're using to submit will perform an HTTP GET on your controller method, as it is changing the document url on click.

Your controller method is restricted to HTTP POST by the [HttpPost] attribute, hence the 404.

What you need to do is use an input of type "submit", which will post back the form to the action specified in the form tag, like so:

<inputtype="submit"class="btn btn-primary" value="Assign"  />

This is the easy and appropriate way to submit form data to the server.

Additionally, you will need to assign a name to the select element to ensure that it's value gets bound to the parameter on your controller method

<select id="template" name="templateId">

Post a Comment for "Submit Form In Mvc Using Html"