【问题标题】:Dropdown in MVC is not loading the data on Edit Function which call by WCF ServiceMVC 中的下拉菜单未加载 WCF 服务调用的编辑函数上的数据
【发布时间】: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


【解决方案1】:

尝试替换此行

@Html.DropDownListFor(m => m.CountryID, ViewBag.ListofCountries as SelectList, "--Select Country--", new { @id = "DropDwnCountry", @class= "form-control" })

@Html.DropDownListFor(m => m.CountryID,new SelectList(ViewBag.ListofCountries, "CountryID", "CountryName"), "--选择国家--", new { @id = "DropDwnCountry", @ class= "表单控制" })

在索引视图中

【讨论】:

  • 试过m错误DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'CountryID'
  • 是的,您应该在您的视图中调用的@modal 中有 CountryId。例如----@model yourProjectName.Model.ViewModel-----.. 你的视图中有这样的东西吗?您可以根据自己的情况调用视图模型或示例模型。
  • 同样的错误,我通过tuple方法调用两个服务对象,同样的错误
猜你喜欢
  • 2019-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多