MVC 5 数据未从视图传递到模式到控制器
MVC 5 Data was not pass to mode from view to controller
我有一个包含 2 个的 productModelView Model.All 当传回控制器中的模型时,产品和库存字段中的数据未填充到模型中。
public class ProductDetails
{
public ProductsTb Product { get; set; }
public InventoryTb ProductInventory { get; set; }
public List<ItemTypeLibrary> ProductTypeList { get; set; }
}
制作代码 short.i 将只显示 productsTB
中的 1 个属性
public ActionResult Create()
{//load up create page to populate product type list
ProductDetails pd = new ProductDetails();
pd.ProductTypeList = db.ItemTypeLibraries.ToList();
return View(pd);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "productID,ArticleID,ProductType,ProductName")] ProductDetails productsTb)
{
if (ModelState.IsValid)
{
db.ProductsTbs.Add(productsTb.Product);
db.SaveChanges();
db.InventoryTbs.Add(productsTb.ProductInventory);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(productsTb);
}
@model RicmasNet.Models.Product.ProductDetails
创建
@model RicmasNet.Models.Product.ProductDetails
@using (Html.BeginForm()) {
<div class="form-horizontal">
<h4>ProductsTb</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Product.ArticleID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ArticleID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ArticleID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model =>model.Product.ProductType, new SelectList(Model.ProductTypeList,"ItemTypeId","ItemTypeName"), "Select a Type...", new { @style = "width:500px" })
@* @Html.EditorFor(model => model.ProductType, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.ValidationMessageFor(model => model.ProductTypeList, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ProductName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductLocation, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductLocation, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ProductLocation, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductBrand, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductBrand, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ProductBrand, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductOrigin, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductOrigin, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ProductOrigin, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductCost, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductCost, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ProductCost, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductRetailPrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductRetailPrice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ProductRetailPrice, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductDealerPrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductDealerPrice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ProductDealerPrice, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductWeight, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductWeight, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductDescEnglish, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductDescEnglish, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductDescNonEnglish, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductDescNonEnglish, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductPicPath, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductPicPath, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
@Html.HiddenFor(model => model.Product.IsDelete)
@Model.Product.IsDelete == true;
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductPicPath2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductPicPath2, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductPicPath3, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductPicPath3, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductVideo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductVideo, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.COC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.COC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.COC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.COCIssue, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.COCIssue, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.COCExpireDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.COCExpireDate, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.COCType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.COCType, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.COCRF, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Product.COCRF)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.EAN, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.EAN, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>}
从您发布的内容中不清楚哪些内容未被绑定,但如果您需要整个视图模型,请删除 Bind(Include...)
属性。在那种情况下不需要它。
另一种选择是,使用 FormCollection 而不是传递模型,以防自定义属性包含对象。
[HttpPost]
public ActionResult Create(FormCollection form)
{
// here you can retrieve attributes from form and assign to your object
productsTb.Id = form["Id"]; // it should match the name of tag in View
return View(productsTb);
}
我有一个包含 2 个的 productModelView Model.All 当传回控制器中的模型时,产品和库存字段中的数据未填充到模型中。
public class ProductDetails
{
public ProductsTb Product { get; set; }
public InventoryTb ProductInventory { get; set; }
public List<ItemTypeLibrary> ProductTypeList { get; set; }
}
制作代码 short.i 将只显示 productsTB
中的 1 个属性 public ActionResult Create()
{//load up create page to populate product type list
ProductDetails pd = new ProductDetails();
pd.ProductTypeList = db.ItemTypeLibraries.ToList();
return View(pd);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "productID,ArticleID,ProductType,ProductName")] ProductDetails productsTb)
{
if (ModelState.IsValid)
{
db.ProductsTbs.Add(productsTb.Product);
db.SaveChanges();
db.InventoryTbs.Add(productsTb.ProductInventory);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(productsTb);
}
@model RicmasNet.Models.Product.ProductDetails
创建
@model RicmasNet.Models.Product.ProductDetails
@using (Html.BeginForm()) {
<div class="form-horizontal">
<h4>ProductsTb</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Product.ArticleID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ArticleID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ArticleID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model =>model.Product.ProductType, new SelectList(Model.ProductTypeList,"ItemTypeId","ItemTypeName"), "Select a Type...", new { @style = "width:500px" })
@* @Html.EditorFor(model => model.ProductType, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.ValidationMessageFor(model => model.ProductTypeList, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ProductName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductLocation, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductLocation, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ProductLocation, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductBrand, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductBrand, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ProductBrand, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductOrigin, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductOrigin, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ProductOrigin, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductCost, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductCost, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ProductCost, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductRetailPrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductRetailPrice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ProductRetailPrice, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductDealerPrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductDealerPrice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ProductDealerPrice, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductWeight, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductWeight, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductDescEnglish, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductDescEnglish, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductDescNonEnglish, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductDescNonEnglish, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductPicPath, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductPicPath, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
@Html.HiddenFor(model => model.Product.IsDelete)
@Model.Product.IsDelete == true;
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductPicPath2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductPicPath2, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductPicPath3, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductPicPath3, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ProductVideo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.ProductVideo, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.COC, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.COC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.COC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.COCIssue, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.COCIssue, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.COCExpireDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.COCExpireDate, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.COCType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.COCType, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.COCRF, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Product.COCRF)
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.EAN, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Product.EAN, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>}
从您发布的内容中不清楚哪些内容未被绑定,但如果您需要整个视图模型,请删除 Bind(Include...)
属性。在那种情况下不需要它。
另一种选择是,使用 FormCollection 而不是传递模型,以防自定义属性包含对象。
[HttpPost]
public ActionResult Create(FormCollection form)
{
// here you can retrieve attributes from form and assign to your object
productsTb.Id = form["Id"]; // it should match the name of tag in View
return View(productsTb);
}