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">
    <div class="control-group">
        <label class="control-label">Template:</label>
        <div class="controls">
            <select id="template" name="myDropDownList">
                @foreach (KeyValuePair<int, string> entry in Model.templates)
                {
                    <option value="@entry.Key">@entry.Value</option>
                }
            </select>
        </div>
    </div>
    <div class="control-group">
        <div class="controls">
            <input type="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:

<input type="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"