Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I am new to MVC3 and i have never worked on MVC before. So need some help.

I am having dropdown in my view. I want to pass dropdown value to controller.

How can i achieve it?

Below is the view

HTML
<div class="editor-label">
           Car Model:
           <select id="CarModel" name="CarModel">
               <option>Select Category</option>
           </select>
       </div>


By some reason, I want to use select as the dropdown list instead of @Html.Dropdownlist or @Html.Dropdownlistfor

Can anyone suggest me the solution for the same, on how to pass value of the above dropdown list to controller?

Thanks in advance.

Regards,
Posted

1 solution

Its pretty simple, In the HttpPost or HttpGet annotated action method have the parameter as CarName. An example is given below:

View:

C#
@using (Html.BeginForm())
{
    <select id="CarModel" name="CarModel">
        <option>Select Category</option>
        <option value="1">one</option>
        <option value="2">two</option>
        <option value="3">three</option>
        <option value="4">four</option>
    </select>
    
    <br />
    
    <input type="submit" value="Submit" />
}

@if (ViewData["Data"] != null)
{
    <h1>@ViewData["Data"].ToString()</h1>
}


Controller:

C#
public ActionResult Dropdown()
{
    return View();
}

[HttpPost]
public ActionResult Dropdown(string CarModel)
{
    ViewData["Data"] = CarModel;
    return View();
}


Hope this helps!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900