在我的應用程序中,我有兩個下拉列表。首先顯示國家值,其次顯示所選國家的狀態。這些值將生成並顯示在兩個下拉列表中。但在發佈後,兩個dropdownlist都會返回模型值的id,而不是名稱或值。如何在後期綁定dropdownlist的選定項目文本? 型號:如何將模型值綁定到下拉列表中的選定項目文本mvc4
public string State { get; set; }
public string Country { get; set; }
public SelectList CountryList { get; set; }
public SelectList RegionList { get; set; }
public class Countries
{
public string ID { get; set; }
public string Name { get; set; }
}
public class Region
{
public string ID { get; set; }
public string Name { get; set; }
}
查看
@Html.DropDownListFor(model =>model.State, new SelectList(Model.RegionList, "Value", "Text", Model.RegionList.SelectedValue))
@Html.DropDownListFor(model => model.Country, new SelectList(Model.CountryList, "Value", "Text", Model.CountryList.SelectedValue), new { data_url = Url.Action("GetRegionDetail", "WPindex") })
<script type="text/javascript">
$(document).ready(function() {
$("#Country").change(function() {
$("#State").empty();
var url =$(this).data(url);
var Id = $('#Country option:selected').attr('value');
$.getJSON(url, { ID: Id },
function (data) {
jQuery.each(data, function (key, Region) {
$("#State").append($("<option></option>").val(Region.ID).html(Region.Name));
}
);
});
});
});
</script>
控制器:
public JsonResult GetRegionDetail(int ID)
{
AddressModel amodel = new AddressModel();
List<Model.Region> objRegion = new List<Model.Region>();
objRegion = GetRegionList(ID);
SelectList objlistofRegiontobind = new SelectList(objRegion, "ID", "Name", 0);
amodel.RegionList = objlistofRegiontobind;
return Json(objRegion, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult UpdateDetails(Model objmodel)
{
string state = objmodel.State; // returns ID and not Name (selected text)
string country = objmodel.Country; // returns ID and not Name
}