这是我的viewModel,我绑定到视图。
public class EmployeeMasterViewModel
{
public EmployeeViewModel Employee { get; set; }
public List<SkillViewModel> Skills { get; set; }
}
Employee
属性绑定成功,所有属性都被填充了
但这是问题Skills
属性,它是SkillViewModel
的列表,
public class SkillViewModel
{
public int Id { get; set; }
public string SkillName { get; set; }
}
提交表单时 Skills.Count
为0
@for (int i = 0; i < Model.Skills.Count; i++)
{
SkillViewModel skill = Model.Skills[i];
string id = skill.Id + "-" + String.Join("-", skill.SkillName.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries));
<tr id="@id">
<td>
@Html.HiddenFor(e => e.Skills[i].Id)
@Html.HiddenFor(e => e.Skills[i].SkillName)
@skill.SkillName
</td>
<td>
<a href="javascript:deleteSkill('@id')">
<span class="label label-sm label-danger">
Delete
</span>
</a>
</td>
</tr>
}
控制器
public ActionResult Create()
{
ViewBag.CanCreateEmployee = Right.CanCreateEmployee;
ViewBag.Skills = Common.GetSkills();
var viewModel = new EmployeeMasterViewModel();
var employee = new EmployeeViewModel
{
Departments = Common.GetDepartments(),
Designations = Common.GetDesignations(),
Grades = Common.GetGrades(),
MaritalStatuses = Common.GetMaritalStatuses(),
Genders = Common.GetGenders(),
};
viewModel.Skills = new List<SkillViewModel>();
viewModel.Skills.Add(new SkillViewModel
{
Id = 1,
SkillName = "Test Driven Developement"
});
viewModel.Employee = employee;
return View(viewModel);
}
POST动作
[HttpPost]
public ActionResult Create(EmployeeMasterViewModel viewModel, HttpPostedFileBase file)
{
if (!Right.CanCreateEmployee)
{
Session["ErrorNotify"] = "You don't have permission to add new employee";
return RedirectToAction("Index");
}
viewModel.Employee.Departments = Common.GetDepartments();
viewModel.Employee.Designations = Common.GetDesignations();
viewModel.Employee.Grades = Common.GetGrades();
viewModel.Employee.MaritalStatuses = Common.GetMaritalStatuses();
viewModel.Employee.Genders = Common.GetGenders();
viewModel.Skills = _model.GetEmployeeSkills();
ViewBag.Skills = Common.GetSkills();
ViewBag.CanCreateEmployee = Right.CanCreateEmployee;
if (!CheckErrors(viewModel))
{
return View(viewModel);
}
string image = Common.ImageUpload(file);
if (image == "0")
{
ModelState.AddModelError("ImageTypeError", "Image type not supported");
return View(viewModel);
}
viewModel.Employee.ImgPath = image;
_model.Insert(viewModel.Employee);
Session["SuccessNotify"] = "Employee is successfully registered";
return RedirectToAction("Index");
}
答案 0 :(得分:0)
我试图复制你的问题,但我的尝试工作正常。
查看代码
@model SO.Controllers.EmployeeMasterViewModel
@using (Html.BeginForm())
{
@Html.TextBoxFor(m => m.Employee.EmployeeName);
<input data-val="true" name="Skills[0].Id" type="hidden" value="1">
<input name="Skills[0].SkillName" type="hidden" value="Test Driven Developement">
<input type="submit" value="Save" />
}
控制器代码
public class HomeController : Controller
{
public ActionResult Index()
{
var vm = new EmployeeMasterViewModel();
vm.Employee = new EmployeeViewModel();
vm.Skills = new List<SkillViewModel>();
return View(vm);
}
[HttpPost]
public ActionResult Index(EmployeeMasterViewModel viewModel)
{
return View(viewModel);
}
}
当我提交表单时,所有数据都已正确传递,包括技能数据。也许你可以用这个答案作为参考来帮助你?