我是ASP MVC的初学者,经过SO的大量帮助后,我正在通过ViewModels进步。但是,使用ViewModel,我遇到了以下错误。
给出以下视图:
@model November.ViewModels.Staff_Salutation_VM
//...
using (Html.BeginForm("UpdateStaff", "Settings", FormMethod.Post,
new { @class = "clearfix parameter-form update-parameter update-staff", enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
//...
@for (int i = 0; i < Model.AvailableStaffMembers.Count; i++)
{
var staff = Model.AvailableStaffMembers[i];
<tr>
<td>@Html.HiddenFor(model => staff.ID)@Html.ValueFor(model => staff.ID)</td>
<td>
@Html.DropDownListFor(
model => model.SalutationID, Model.AvailableSalutations.Select(option => new SelectListItem
{
Text = option.Desc.ToString(),
Value = option.ID.ToString(),
Selected = (option.ID.ToString() == staff.SalutationID.ToString())
}
),
"Choose...")
</td>
<td>@Html.EditorFor(model => staff.FName)</td>
<td>@Html.EditorFor(model => staff.LName)</td>
<td>@Html.EditorFor(model => staff.Active)</td>
<td><a href="/Settings/DeleteStaff?id=@Html.ValueFor(model => staff.ID)">Delete</a></td>
</tr>
}
以及以下控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using November.Models;
using November.ViewModels;
using November.DAL;
//...
//GET
var staffCreateViewModel = new Staff_Salutation_VM();
staffCreateViewModel.AvailableSalutations = new List<Prm_Salutation>();
var activeSalts = (from a in db.Prm_Salutations
where a.Active == true
orderby a.Desc ascending
select a);
staffCreateViewModel.AvailableSalutations = activeSalts.ToList();
staffCreateViewModel.AvailableStaffMembers = new List<Prm_Staff>();
var activeStaff = (from a in db.Prm_Staffs
where a.Active == true
orderby a.LName ascending
select a);
staffCreateViewModel.AvailableStaffMembers = activeStaff.ToList();
return View("StaffMembers", staffCreateViewModel);
//POST
public ActionResult UpdateStaff(Staff_Salutation_VM list)
{
if (ModelState.IsValid)
{
foreach (var formData in list) //no longer works due to dropping List<>
{
var tbl = db.Prm_Staffs.Where(a => a.ID.Equals(formData.ID)).FirstOrDefault();
if (tbl != null)
{
var Prm_StaffModel = new Prm_Staff();
Prm_StaffModel.SalutationID = formData.SalutationID;
Prm_StaffModel.FName = formData.FName;
Prm_StaffModel.LName = formData.LName;
Prm_StaffModel.Active = formData.Active;
}
}
db.SaveChanges();
ViewBag.UpdateRtrn = "Successfully Updated.";
return RedirectToAction("Parameters", new { param = "Staff Members" });
}
else
{
ViewBag.UpdateRtrn = "Failed ! Please try again.";
return RedirectToAction("Parameters", new { param = "Staff Members" });
}
}
return RedirectToAction("Parameters", new { param = "Staff Members" });
}
并且,为了更好的衡量,ViewModel本身:
public class Staff_Salutation_VM
{
public int ID { get; set; }
public int SalutationID { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public bool Active { get; set; }
public List<Prm_Salutation> AvailableSalutations { get; set; }
public List<Prm_Staff> AvailableStaffMembers { get; set; }
public Staff_Salutation_VM() { }
}
触发时,没有任何表单值填充ActionResult,导致在达到Object reference not set to an instance of an object.
行时抛出foreach (var formData in list)
异常。调试显示list
为空。怎么会这样呢?或者说,我做错了什么?
编辑:我的POST list
中的ActionResult
变量现在正在获取数据 - 或者至少在调试时显示了类中的各种类型。然后我如何迭代它以将数据保存在DB的适当行中?
答案 0 :(得分:1)
我完全错过了方法签名,对不起!您的初始视图加载会传递模型Staff_Salutation_VM
,但您的UpdateStaff(已发布表单)需要List<Staff_Salutation_VM>
。这些是不同的动物。将public ActionResult UpdateStaff(List<Staff_Salutation_VM> list)
更改为public ActionResult UpdateStaff(Staff_Salutation_VM staff)
只是为了查看您是否超过null ref
例外。请注意,由于您没有进入IEnumerable,因此您需要删除您的foreach。
答案 1 :(得分:0)
我希望这篇文章对你有所帮助。 Model Binding To A List