【发布时间】:2023-03-17 05:40:01
【问题描述】:
从 WCF 服务调用时,MVC 中的下拉菜单未在 Edit Function 上加载数据, 在下拉列表中列出了整个国家/地区名称,但是,在记录保存数据时,下拉列表中未显示已保存的所选国家/地区,请查找所附图片
编辑功能点击索引页面中的以下代码(仅下拉值显示不正确)
<form id="form">
<fieldset id="SubmitForm">
<div class="form-group">
@Html.DropDownListFor(m => m.CountryID, ViewBag.ListofCountries as SelectList, "--Select Country--", new { @id = "DropDwnCountry", @class = "form-control" })
</div>
</fieldset>
代码是编辑功能
var Edit = function (UserID) {
$("#ModelTitle").html("Update Record");
$("#MyModal").modal();
$.ajax({
type: "POST",
url: "/User/GetUserbyID?UserID=" + UserID,
success: function (data) {
var obj = JSON.parse(data);
$("#UserID").val(obj.UserID);
$("#userCode").val(obj.UserCode);
$("#userName").val(obj.Name);
debugger;
alert('1');
//$("#DropDwnCountry option:selected").text(ViewBag.ListofCountries)
$("#DropDwnCountry option:selected").val(obj.CountryID); //not loading the correct value
alert(obj.CountryID);
//alert($("#DropDwn").text(obj.CountryID));
}
})
}
以下代码,在控制器中
public ActionResult Index()
{
Countries = new TktServiceClient().GetCountries().ToList();
ViewBag.ListofCountries = new SelectList(Countries, "CountryID","CountryName" );
return View();
}
请帮助解决这个问题所面临的错误
【问题讨论】:
-
如果您使用Postman发送Post请求,该对象是否包含CountryID属性?或者,数据库中的条目是否具有 CountryID 属性?
-
是的,显示的是正确的ID,但没有显示文字
标签: asp.net-mvc asp.net-mvc-4 model-view-controller asp.net-mvc-5 wcf-data-services