4
Reply

how to get dropdownlist selected value on controller action

Partho Rocko

Partho Rocko

Aug 20 2016 5:34 AM
257
//Model
public class ObjectModel
{
[Required(ErrorMessage = "Please Select District")]
[Display(Name = "Select District")]
public IList<SelectListItem> DistrictNamesModel { get; set; }
public int DistrictId { get; set; }
}
//View
@model MVCtrendsetters.Models.ObjectModel
@{
ViewBag.Title = "Insert";
}
<h2>Insert</h2>
@using (Html.BeginForm("Insert", "AppForm", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>ObjectModel</legend>
<div>
@Html.DropDownListFor(x => x.DistrictNamesModel, Model.DistrictNamesModel , "--Select--", new { @id = "ddlDistrict"});
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
//Controller
ObjectModel OM = new ObjectModel();
DB dd = new DB();
DataSet ds = new DataSet();
ds = dd.getAllDistrict();
List<SelectListItem> DistrictNames = new List<SelectListItem>();
foreach (DataRow item in ds.Tables[0].Rows)
{
DistrictNames.Add(new SelectListItem { Text = item["CityName"].ToString(), Value = (item["Id"].ToString()) });
}
OM.DistrictNamesModel = DistrictNames;
return View(OM);
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Insert(ObjectModel OM , string DistrictId)
{
return View(OM);
}
Myy question Is How will i get DistrictId in [Httpost] Insert method.. I used a dropdown which is filled correctly. My table has Column Name City adn Id....PLEASE HELP....I will be really thankful

Answers (4)