我正在运行 Visual Studio 2012 调试器,并且发现我的 productViewModelList 参数不包含我希望我的视图传递给我的 [HTTPPOST] 编辑操作的所有值。我不明白为什么。请参考下面代码示例中的注释,了解我插入断点并检查 productViewModelList 值的位置。
为 productViewModelList 提供了以下值:
BrandId = 0,BrandName = "6",BrandSelectListItem = null,ID = 5,Name = "Crutch",价格 = 10.0
- BrandID 不正确,在我的 DropDownList 的视图中,我指定了“Catatonics Inc.”。它的 ID 为 6,我在我的数据库中进行了验证。
- BrandName 显示“6”,应在 BrandID 中,BrandName 应为“Catatonics Inc.”。
- BrandSelectList 项目是 SelectListItem 类型的对象,它包含在我的视图中进入 DropDownList 项目的值。DropDownList 正确显示了值,但 BrandSelectList 是
null
我的 [httpPost] 编辑操作执行时。我需要访问 DropDownList 的 Selected 项。 - 所有其他值、ID、名称和价格都是正确的。
这是我的代码中的一些类。
医疗产品控制器
public class MedicalProductController : Controller
{
private MvcMedicalStoreDb _db = new MvcMedicalStoreDb();
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(IEnumerable<MedicalProductViewModel> productViewModelList)
{
// I have a breakpoint inserted here, and check productViewModelList with debugger.
var modelList = GetMedicalProductList(productViewModelList);
if (ModelState.IsValid)
{
foreach (var model in modelList)
_db.Entry(model).State = EntityState.Modified;
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(productViewModelList);
}
}
医疗产品映射器
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 = Convert.ToInt32(viewModel.BrandSelectListItem.Select(b => b.Value.ToString()))
});
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")
}
EditorTemplates/MedicalProductViewModel.cshtml
(这是在 Edit.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.BrandName)
</div>
<div class="editor-field">
@Html.DropDownListFor(item => Model.BrandName, Model.BrandSelectListItem)
@Html.ValidationMessageFor(item => Model.BrandName)
</div>
编辑:
品牌
public class Brand
{
[Key]
public int ID { get; set; }
[Required]
[StringLength(30)]
public string Name { get; set; }
}
医疗产品
public class MedicalProduct
{
[Key]
public int ID { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
[Required]
[DataType(DataType.Currency)]
public double Price { get; set; }
// is a foreign key
public int BrandID { get; set; }
}
医疗产品视图模型
public class MedicalProductViewModel
{
[Key]
public int ID { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
[Required]
[DataType(DataType.Currency)]
public double Price { get; set; }
public int BrandID { get; set; }
[DisplayFormat(NullDisplayText="[Generic]")]
public string BrandName { get; set; }
public IEnumerable<SelectListItem> BrandSelectListItem { get; set; }
}