【发布时间】:2021-12-19 04:53:31
【问题描述】:
我正在尝试使用 EF 4.7.2 和继承为我的 PC 组件 ASP.NET MVC 应用程序实现创建功能,以便在单个方法中处理所有派生类。
问题在于提交Component_CreateCPU.cshtml 表单会将CPU 的派生类在/Components/Create 操作中转换为其基类Component。
我测试了在Index() 中实例化一个新的CPU 对象并将其传递给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");
}
}
【问题讨论】:
标签: c# asp.net-mvc entity-framework inheritance