@Html.DropDownListFor 绑定给出 "The parameter conversion from type 'System.String' to type" 错误
@Html.DropDownListFor binding giving "The parameter conversion from type 'System.String' to type" error
我只是想将 @Html.DropDownListFor 绑定到我的视图模型,每当我添加新产品时,我都会收到错误消息:
"The parameter conversion from type 'System.String' to type 'SuperStockpile.Core.Models.Brand' failed because no type converter can convert between these types."
基础实体
public abstract class BaseEntity
{
public string Id { get; set; }
[Display(Name = "Created At")]
public DateTimeOffset CreatedAt { get; set; }
public BaseEntity()
{
Id = Guid.NewGuid().ToString();
CreatedAt = DateTime.Now;
}
}
品牌型号
public class Brand : BaseEntity
{
public string Name { get; set; }
}
查看模型
public class ProductFormViewModel
{
public Product Product { get; set; }
public IEnumerable<Brand> Brands { get; set; }
}
库存控制器
public class InventoryController : Controller
{
private readonly IRepository<Product> _productContext;
private readonly IRepository<Brand> _brandContext;
private readonly IRepository<Source> _sourceContext;
public InventoryController(IRepository<Product> productContext, IRepository<Brand> brandContext,
IRepository<Source> sourceContext)
{
_productContext = productContext;
_sourceContext = sourceContext;
_brandContext = brandContext;
}
// GET: Inventory
public ActionResult Index()
{
List<Product> Products = _productContext.Collection().ToList();
return View(Products);
}
public ActionResult Create()
{
ProductFormViewModel product = new ProductFormViewModel
{
Product = new Product(),
Brands = _brandContext.Collection().ToList(),
Sources = _sourceContext.Collection().ToList()
};
return View(product);
}
[HttpPost]
public ActionResult Create(ProductFormViewModel vm)
{
if (!ModelState.IsValid)
return View("Create", vm);
else
{
Product product = new Product
{
Brand = vm.Product.Brand,
BrandId = vm.Product.BrandId,
Comments = vm.Product.Comments,
Cost = vm.Product.Cost,
DiscountPercent = vm.Product.DiscountPercent,
ItemCode = vm.Product.ItemCode,
Source = vm.Product.Source,
SourceId = vm.Product.SourceId,
SKU = vm.Product.SKU,
UPC = vm.Product.UPC
};
_productContext.Insert(product);
_productContext.Commit();
return RedirectToAction("Index");
}
}
}
查看
@model SuperStockpile.Core.ViewModels.ProductFormViewModel
@{
ViewBag.Title = "Create";
}
<h2>Add New Product</h2>
@using (Html.BeginForm("Create","Inventory"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Product.SKU)
@Html.EditorFor(model => model.Product.SKU, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.SKU)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.UPC)
@Html.EditorFor(model => model.Product.UPC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.UPC)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ItemCode)
@Html.EditorFor(model => model.Product.ItemCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ItemCode)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.Cost)
@Html.EditorFor(model => model.Product.Cost, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.Cost)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.DiscountPercent)
@Html.EditorFor(model => model.Product.DiscountPercent, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.DiscountPercent)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.Comments)
@Html.EditorFor(model => model.Product.Comments, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.Comments)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.BrandId)
@Html.DropDownListFor(model => model.Product.Brand, new SelectList(Model.Brands, "Id", "Name"),"Select Brand", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Product.BrandId)
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我不确定如何将下拉列表正确绑定到我的视图模型品牌 属性。任何关于为什么会发生这种情况的帮助或解释都会很棒!
感谢您的观看。
不要整体绑定到品牌 class,只绑定到 Brand.Id,稍后在控制器中获取品牌的剩余详细信息,然后再保存到数据库。
@Html.DropDownListFor(model => model.Product.Brand.Id, new SelectList(Model.Brands, "Id", "Name"),"Select Brand", new { @class = "form-control" })
我只是想将 @Html.DropDownListFor 绑定到我的视图模型,每当我添加新产品时,我都会收到错误消息:
"The parameter conversion from type 'System.String' to type 'SuperStockpile.Core.Models.Brand' failed because no type converter can convert between these types."
基础实体
public abstract class BaseEntity
{
public string Id { get; set; }
[Display(Name = "Created At")]
public DateTimeOffset CreatedAt { get; set; }
public BaseEntity()
{
Id = Guid.NewGuid().ToString();
CreatedAt = DateTime.Now;
}
}
品牌型号
public class Brand : BaseEntity
{
public string Name { get; set; }
}
查看模型
public class ProductFormViewModel
{
public Product Product { get; set; }
public IEnumerable<Brand> Brands { get; set; }
}
库存控制器
public class InventoryController : Controller
{
private readonly IRepository<Product> _productContext;
private readonly IRepository<Brand> _brandContext;
private readonly IRepository<Source> _sourceContext;
public InventoryController(IRepository<Product> productContext, IRepository<Brand> brandContext,
IRepository<Source> sourceContext)
{
_productContext = productContext;
_sourceContext = sourceContext;
_brandContext = brandContext;
}
// GET: Inventory
public ActionResult Index()
{
List<Product> Products = _productContext.Collection().ToList();
return View(Products);
}
public ActionResult Create()
{
ProductFormViewModel product = new ProductFormViewModel
{
Product = new Product(),
Brands = _brandContext.Collection().ToList(),
Sources = _sourceContext.Collection().ToList()
};
return View(product);
}
[HttpPost]
public ActionResult Create(ProductFormViewModel vm)
{
if (!ModelState.IsValid)
return View("Create", vm);
else
{
Product product = new Product
{
Brand = vm.Product.Brand,
BrandId = vm.Product.BrandId,
Comments = vm.Product.Comments,
Cost = vm.Product.Cost,
DiscountPercent = vm.Product.DiscountPercent,
ItemCode = vm.Product.ItemCode,
Source = vm.Product.Source,
SourceId = vm.Product.SourceId,
SKU = vm.Product.SKU,
UPC = vm.Product.UPC
};
_productContext.Insert(product);
_productContext.Commit();
return RedirectToAction("Index");
}
}
}
查看
@model SuperStockpile.Core.ViewModels.ProductFormViewModel
@{
ViewBag.Title = "Create";
}
<h2>Add New Product</h2>
@using (Html.BeginForm("Create","Inventory"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Product.SKU)
@Html.EditorFor(model => model.Product.SKU, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.SKU)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.UPC)
@Html.EditorFor(model => model.Product.UPC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.UPC)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.ItemCode)
@Html.EditorFor(model => model.Product.ItemCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.ItemCode)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.Cost)
@Html.EditorFor(model => model.Product.Cost, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.Cost)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.DiscountPercent)
@Html.EditorFor(model => model.Product.DiscountPercent, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.DiscountPercent)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.Comments)
@Html.EditorFor(model => model.Product.Comments, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Product.Comments)
</div>
<div class="form-group">
@Html.LabelFor(model => model.Product.BrandId)
@Html.DropDownListFor(model => model.Product.Brand, new SelectList(Model.Brands, "Id", "Name"),"Select Brand", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Product.BrandId)
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我不确定如何将下拉列表正确绑定到我的视图模型品牌 属性。任何关于为什么会发生这种情况的帮助或解释都会很棒!
感谢您的观看。
不要整体绑定到品牌 class,只绑定到 Brand.Id,稍后在控制器中获取品牌的剩余详细信息,然后再保存到数据库。
@Html.DropDownListFor(model => model.Product.Brand.Id, new SelectList(Model.Brands, "Id", "Name"),"Select Brand", new { @class = "form-control" })