I have dropdownlist, which I have filled from database. Now I need to get the selected value in Controller do some manipulation. But not getting the idea. Code which I have tried.
Model
public class MobileViewModel
{
public List<tbInsertMobile> MobileList;
public SelectList Vendor { get; set; }
}
Controller
public ActionResult ShowAllMobileDetails()
{
MobileViewModel MV = new MobileViewModel();
MV.MobileList = db.Usp_InsertUpdateDelete(null, "", "", null, "", 4, MergeOption.AppendOnly).ToList();
MV.Vendor = new SelectList(db.Usp_VendorList(), "VendorId", "VendorName");
return View(MV);
}
[HttpPost]
public ActionResult ShowAllMobileDetails(MobileViewModel MV)
{
string strDDLValue = ""; // Here i need the dropdownlist value
return View(MV);
}
View
<table>
<tr>
<td>Mobile Manufacured</td>
<td>@Html.DropDownList("ddlVendor", Model.Vendor, "Select Manufacurer") </td>
</tr>
<tr>
<td>
</td>
<td>
<input id="Submit1" type="submit" value="search" />
</td>
</tr>
</table>
I was having the same issue in asp.NET razor C#
I had a
ComboBox
filled with titles from anEventMessage
, and I wanted to show the Content of this message with its selected value to show it in a label orTextField
or any otherControl
...My
ComboBox
was filled like this:In my
EventController
I had a function to go to the page, in which I wanted to show myComboBox
(which is of a different model type, so I had to use a partial view)?The function to get from index to page in which to load the partial view:
The function for the partial view is here:
The function to fill EventBerichten:
The partialView Model looks like this:
VIEUW: This is the script used to get my output in the view
1st Approach (via Request or FormCollection):
You can read it from
Request
usingRequest.Form
, your dropdown name isddlVendor
so passddlVendor
key in the formCollection to get its value that is posted by form:or Use
FormCollection
:2nd Approach (Via Model):
If you want with Model binding then add a property in Model:
and in View:
and in Action:
UPDATE:
If you want to post the text of selected item as well, you have to add a hidden field and on drop down selection change set selected item text in the hidden field:
use jquery to set hidden field:
MVC 5/6/Razor Pages
I think the best way is with strongly typed model, because Viewbags are being aboused too much already :)
MVC 5 example
Your Get Action
Your View Model
Your View
Your Post Action
MVC 6 It'll be a little different
Get Action
Your View Model will be same as MVC 5
Your View will be like
Post will also be same
Razor Pages
Your Page Model
I hope it may help someone :)
Use SelectList to bind @HtmlDropdownListFor and specify selectedValue parameter in it.
http://msdn.microsoft.com/en-us/library/dd492553(v=vs.108).aspx
Example : you can do like this for getting venderid
Model
Very basic model with Gender field.
GetGenderSelectItems()
returns select items needed to populate DropDownList.View
Please make sure you wrapped your
@Html.DropDownListFor
in a form tag.Controller
Your .cshtml Razor view name should be the same as controller action name and folder name should match controller name e.g
Views\MyController\MyAction.cshtml
.Going further
Now let's make it strongly-typed and enum independent:
Thanks - this helped me to understand better ansd solve a problem I had. The JQuery provided to get the text of selectedItem did NOT wwork for me I changed it to