how to get selected item text in razor dropdownlist.
-
<form id="MonthlyAward" action="@Url.Action("SubmitAwards", "MonthlyAward")" method="post"> @Html.DropDownListFor(m => m.ddlProjectsID, Model.ProjectNames, "--Select--", new { id = "ddlProjects",name="ddl_ProjectnameText", onchange = "document.getElementById('TheForm').submit();" }) how to get selected item text in form collection
[HttpPost]
public ActionResult SubmitAwards(FormCollection collection, MonthlyAwardModel award)
{
string strProjectId = collection["ddlProjectsID"].ToString().Trim();
return View(); -
<form id="MonthlyAward" action="@Url.Action("SubmitAwards", "MonthlyAward")" method="post"> @Html.DropDownListFor(m => m.ddlProjectsID, Model.ProjectNames, "--Select--", new { id = "ddlProjects",name="ddl_ProjectnameText", onchange = "document.getElementById('TheForm').submit();" }) how to get selected item text in form collection
[HttpPost]
public ActionResult SubmitAwards(FormCollection collection, MonthlyAwardModel award)
{
string strProjectId = collection["ddlProjectsID"].ToString().Trim();
return View();There are two ways. 1. Have a hidden field in the page and set the value of that field as $("#hiddendfield").val($("#ddlProjectsID option:selected").text()); You need to this on OnChange event of the dropdown. after that call the form submit method. it will be better if you use $.ajax of the jquery for submission. Then in by using formcollection you can get the value of that hidden field in the controller. 2. have the list again in the controller and fire a query in that list against the id selected. now you have the selected item and you can get the display text