gpt4 book ai didi

javascript - 数据绑定(bind) : 'System.String' does not contain a property with the name 'numeroGuia'

转载 作者:行者123 更新时间:2023-11-27 22:57:24 30 4
gpt4 key购买 nike

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

我正在创建一个部分 View “AgregaGuia”,在其中我对还没有“fechaRecepcionGuia”的行的 TblGuias 模型进行查询,这些指南填充在组合框中,当选择此指南时,指南将填充所有文本框如此看来。但是,在运行应用程序时,它生成以下错误:DataBinding:“System.String”不包含名为“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; }
}

这是我的 Controller :

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>

最佳答案

问题出在 Controller 中的这一行:

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 ,表示要渲染到 <option> 中的项目列表<select> 内的标签.
  • 第二个参数,dataValueField ,是枚举中的项目的属性名称,它将成为 value每个 <option> 内的属性标签。
  • 同样,第三个参数dataTextField ,是属性的名称,它将成为每个 <option> 显示的文本。 .

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

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

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

关于javascript - 数据绑定(bind) : 'System.String' does not contain a property with the name 'numeroGuia' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37468588/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com