【问题标题】:DataBinding: 'System.String' does not contain a property with the name 'numeroGuia'DataBinding:“System.String”不包含名为“numeroGuia”的属性
【发布时间】:2016-05-26 18:34:25
【问题描述】:

我是 ASP.net MVC5 的新手,我的问题是:

我正在创建一个局部视图“Agregaguia”,其中我对尚未拥有“FechareCepcionGuia”的行的TBLGuias模型进行了查询,这些指南填写在组合框中,并选择此指南填充所有文本框在那个视图中。但是,在运行应用程序时,它会产生以下错误:DataBinding : 'System.String' does not Contain a property with name 'numeroGuia'。

谁能帮帮我?

这是我的模型:

    public partial class TblGuias
    {
        public TblGuias()
        {
            this.TblFactIC = new HashSet<TblFactIC>();
        }

        public string numeroGuia { get; set; }
        public string companiaEnvios { get; set; }
        public string destino { get; set; }
        public decimal pesoGuia { get; set; }
        public System.DateTime fechaEnvioGuia { get; set; }
        public Nullable<System.DateTime> fechaRecepcionGuia { get; set; }
        public string comprobante { get; set; }

        public virtual ICollection<TblFactIC> TblFactIC { get; set; }
    }

这是我的控制器:

public class vueInveEntrsController : Controller
{

    public ActionResult AgregaGuia()
    {
        ViewData["guia"] = new SelectList(db.TblGuias.Where(g => g.fechaRecepcionGuia == null).Select((g => g.numeroGuia)),"numeroGuia", "companiaEnvios","destino","pesoGuia","fechaEnvioGuia");
        return PartialView(db.TblGuias.ToList());
    }

    [HttpPost]
    public ActionResult Action(string numero)
    {
        var query = from c in db.TblGuias
                    where c.numeroGuia == numero
                    select c;
        return Json(query);
    }

}

我的看法如下:

@using (@Html.BeginForm("Action", "vueInveEntrs", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="form-group">
        @Html.Label("Seleccione Guia", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">

            @Html.DropDownList("numero", (SelectList)ViewData["guia"], new { onchange = "Action(this.value);", @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.Label("Compañia Envios", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBox("transporte", null, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.Label("Destino", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBox("destino", null, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.Label("Peso", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBox("peso", null, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.Label("Fecha Envio", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBox("fechaenvio", null, new { @class = "form-control" })
        </div>
    </div>
}


<script type="text/javascript">
    function Action(numero) {
        $.ajax({
            url: '@Url.Action("Action", "vueInveEntrs")',
            type: "POST",
            data: { "numero": numero },
            "success": function (data) {
                if (data != null) {
                    var vdata = data;
                    $("#transporte").val(vdata[0].companiaEnvios);
                    $("#destino").val(vdata[0].destino);
                    $("#peso").val(vdata[0].pesoGuia);
                    $("#fechaenvio").val(vdata[0].fechaEnvioGuia);
                }
            }
        });
    }
</script>

【问题讨论】:

    标签: javascript asp.net-mvc-5 json.net


    【解决方案1】:

    问题是控制器中的这一行:

    ViewData["guia"] = new SelectList(
            db.TblGuias.Where(g => g.fechaRecepcionGuia == null).Select((g => g.numeroGuia)),
            "numeroGuia", "companiaEnvios","destino","pesoGuia","fechaEnvioGuia");
    

    您没有正确指定SelectList 的构造函数参数。有几种不同的重载,但我认为你想要的是this one

    public SelectList(
        IEnumerable items,
        string dataValueField,
        string dataTextField
    )
    
    • 第一个参数items表示您希望在&lt;select&gt;内的&lt;option&gt;标签中呈现的项目列表。
    • 第二个参数dataValueField是可枚举项的属性名称,它将成为每个&lt;option&gt;标签内的value属性。
    • 同样,第三个参数dataTextField 是属性的名称,它将成为每个&lt;option&gt; 显示的文本。

    因此,如果您将代码更改为以下内容,我认为它应该可以工作:

    ViewData["guia"] = new SelectList(
        db.TblGuias.Where(g => g.fechaRecepcionGuia == null), "numeroGuia", "numeroGuia");
    

    如果您希望在下拉列表中显示不同的文本,请将第三个参数更改为与您的 TblGuias 类不同的属性。

    【讨论】:

      猜你喜欢
      • 2018-03-21
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多