我正在嘗試使用 EF 4.7.2 和繼承為我的 PC 組件 ASP.NET MVC 應用程式實作創建功能,以在單個方法中處理所有派生類。
問題提交Component_CreateCPU.cshtml形式轉換的派生類CPU到它的基類Component的/Components/Create動作。
我測驗了實體化一個新CPU物件Index()并將其傳遞給Create()方法,它保留了它的派生類。
有沒有辦法提交視圖表單并確保派生類被傳入?
模型類:
public class Component : Interfaces.IComponent
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[DisplayName("Name")]
public string Name { get; set; }
[DisplayName("Description")]
public string Description { get; set; }
[DisplayName("Price")]
public decimal Price { get; set; }
public Manufacturer Manufacturer { get; set; }
}
public class CPU : Component
{
[DisplayName("Core Count")]
public int CoreCount { get; set; }
[DisplayName("Core Clock")]
public string CoreClock { get; set; }
}
創建區域視圖
_Component_CreateCPU.cshtml:
@model PCDB.Models.Components.CPU
@using (Html.BeginForm("Create", "Components", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>CPU</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CoreCount, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CoreCount, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CoreCount, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CoreClock, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CoreClock, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CoreClock, "", new { @class = "text-danger" })
</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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
ComponentsController:
public class ComponentsController : Controller
{
private readonly IComponentRepository<Component> _componentRepository;
public ComponentsController()
{
_componentRepository = new ComponentsRepository<Component>();
}
public ActionResult Index()
{
return View(_componentRepository.GetAll());
}
[Authorize(Roles = "Admin")]
public ActionResult Create()
{
return View(new ComponentCreateViewModel());
}
[Authorize(Roles = "Admin")]
[HttpPost]
public ActionResult Create(Component component)
{
if (ModelState.IsValid)
{
_componentRepository.Insert(component);
_componentRepository.Save();
}
return Content("Success");
}
}
uj5u.com熱心網友回復:
當您將表單發布到控制器時,瀏覽器不會序列化物體,它只是傳遞與您的控制器方法接受的物件的屬性或該控制器方法中引數的欄位名稱相結合的欄位。
因此,在您的情況下,您的控制器需要一個基類 Component,這就是它所接收的全部內容,而不是 CPU 或其他子類的實體。(盡管測驗 Component 會發生什么可能是值得的abstract)我的建議是為每個子類實作方法。如果有適用于組件級別的位,請在收到子類后將其傳遞給公共方法:
[Authorize(Roles = "Admin")]
[HttpPost]
public ActionResult CreateCPU(CPU cpu)
{
if (ModelState.IsValid)
{
_componentRepository.Insert(cpu);
_componentRepository.Save();
}
return Content("Success");
}
ComponentRepository 仍然可以接受,Insert(Component)前提是它可以最終確保DbSet參考正確,或者DbContext.Entity<T>將決議傳入的 CPU 與其他組件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/349412.html
標籤:C# asp.net-mvc 实体框架 遗产
上一篇:如何在子類和父類的頭中指定建構式
