在我的MedicalProductController
中,我试图通过传递给视图使我的Create
和Delete
操作能够在一个页面上创建和删除多个对象。IEnumerable<ViewModel>
我在操作中使用的方法与我在此堆栈溢出问题Create
中的操作一起使用。但它不适用于我的行动,所以我怀疑有些地方不对劲。Edit
create
问题是页面上没有出现任何字段。该页面只是在该页面上显示:“创建”、“保存”(按钮)、“返回列表”(可点击的链接)。在“创建”和“保存”之间应该有由 MedicalProductViewModel.cshtml 创建的 html 编辑器字段和标签,如下所示。
这是我的设置的样子。
创建.cshtml
@model IEnumerable<MvcMedicalStore.Models.MedicalProductViewModel>
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>MedicalProduct</legend>
@Html.EditorFor(m => m)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
视图\MedicalProduct\EditorTemplates\MedicalProductViewModel.cshtml
@model MvcMedicalStore.Models.MedicalProductViewModel
@Html.HiddenFor(item => Model.ID)
<div class="editor-label">
@Html.LabelFor(item => Model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(item => Model.Name)
@Html.ValidationMessageFor(item => Model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(item => Model.Price)
</div>
<div class="editor-field">
@Html.EditorFor(item => Model.Price)
@Html.ValidationMessageFor(item => Model.Price)
</div>
<div class="editor-label">
@Html.LabelFor(item => Model.ID)
</div>
<div class="editor-field">
@Html.DropDownListFor(item => Model.BrandID, Model.BrandSelectListItem)
@Html.ValidationMessageFor(item => Model.BrandID)
</div>
MedicalProductController.cs
public class MedicalProductController : Controller
{
private MvcMedicalStoreDb _db = new MvcMedicalStoreDb();
public ActionResult Index()
{
var productList = _db.Products.ToArray();
var viewModelList = GetMedicalProductViewModelList(productList);
return View(viewModelList);
}
public IEnumerable<MedicalProductViewModel> GetMedicalProductViewModelList(IEnumerable<MedicalProduct> productList)
{
var brandList = _db.Brands.ToArray();
var mapper = new MedicalProductMapper();
return mapper.MapMedicalProductViewModel(productList, brandList);
}
public IEnumerable<MedicalProduct> GetMedicalProductList(IEnumerable<MedicalProductViewModel> viewModelList)
{
var mapper = new MedicalProductMapper();
return mapper.MapMedicalProductList(viewModelList);
}
//
// GET: /MedicalSupply/Create
public ActionResult Create()
{
return View();
}
//
// POST: /MedicalSupply/Create
[HttpPost]
[ValidateAntiForgeryToken]
//public ActionResult Create(MedicalProduct medicalProduct)
public ActionResult Create(IEnumerable<MedicalProductViewModel> productViewModelList)
{
var productList = GetMedicalProductList(productViewModelList);
if (ModelState.IsValid)
{
foreach (MedicalProduct product in productList)
_db.Products.Add(product);
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(productViewModelList);
}
}
MedicalProductMapper.cs
public class MedicalProductMapper
{
public IEnumerable<MedicalProductViewModel> MapMedicalProductViewModel(IEnumerable<MedicalProduct> productList, IEnumerable<Brand> brandList)
{
var brandSelectListItem = brandList.Select(b => new SelectListItem()
{
Text = b.Name,
Value = b.ID.ToString()
});
var viewModelList = productList.Select(p => new MedicalProductViewModel()
{
BrandID = p.BrandID,
//BrandName = brandList.SingleOrDefault(b => b.ID == p.BrandID).Name,
BrandSelectListItem = brandSelectListItem,
ID = p.ID,
Price = p.Price,
Name = p.Name
});
return viewModelList;
}
public IEnumerable<MedicalProduct> MapMedicalProductList(IEnumerable<MedicalProductViewModel> viewModelList)
{
var modelList = viewModelList.ToArray().Select( viewModel => new MedicalProduct()
{
Name = viewModel.Name,
Price = viewModel.Price,
BrandID = viewModel.BrandID
});
return modelList;
}
}
编辑
我的编辑视图与我的创建视图非常相似:
编辑.cshtml
@model IEnumerable<MvcMedicalStore.Models.MedicalProductViewModel>
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>MedicalProduct</legend>
@Html.EditorFor(m => m)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}