我是ASP.net MVC5的新手,我的问题是:
我正在创建一个部分视图" AgregaGuia",其中我查询了尚未拥有的行的TblGuias模型" fechaRecepcionGuia",这些指南填写了组合框,当选择此选项时,该指南将填充该视图中的所有文本框。但是,在运行应用程序时,它会生成以下错误: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; }
}
这是我的控制者:
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>
答案 0 :(得分: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
表示您希望在<option>
内呈现<select>
个标记的项目列表。dataValueField
是枚举项中属性的名称,它将成为每个value
标记内的<option>
属性。dataTextField
是属性的名称,该属性将成为每个<option>
显示的文本。因此,如果您将代码更改为以下内容,我认为它应该有效:
ViewData["guia"] = new SelectList(
db.TblGuias.Where(g => g.fechaRecepcionGuia == null), "numeroGuia", "numeroGuia");
如果您希望在下拉列表中显示不同的文本,请将第三个参数更改为与TblGuias
类不同的属性。