我完全不明白这个简单的概念如何似乎不起作用。我有一个 ViewModel、一个控制器和一个视图...
这是我的基本视图模型
public class CreateProfessionalEducationViewModel
{
public Models.ProfessionalEducation ProfessionalEducation;
public int ConsultantId;
public CreateProfessionalEducationViewModel() {} // parameterless constr
}
这些是我的 2 个控制器方法
public ActionResult Create(int id)
{
var viewModel = new CreateProfessionalEducationViewModel
{
ConsultantId = id
};
return View(viewModel);
}
//
// POST: /ProfessionalEducation/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CreateProfessionalEducationViewModel collection)
{
if (ModelState.IsValid)
{
//db.ProfessionalEducations.Add(professionaleducation);
db.SaveChanges();
return RedirectToAction("Index");
}
return View();
}
和我的看法...
@model Axxes_Virtual_Resume_MVC.ViewModel.ProfessionalEducation.CreateProfessionalEducationViewModel
@using (Html.BeginForm("Create", "ProfessionalEducation")) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>ProfessionalEducation</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ProfessionalEducation.Year)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProfessionalEducation.Year)
@Html.ValidationMessageFor(model => model.ProfessionalEducation.Year)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProfessionalEducation.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProfessionalEducation.Title)
@Html.ValidationMessageFor(model => model.ProfessionalEducation.Title)
</div>
@Html.HiddenFor(model => model.ConsultantId)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
由于某些奇怪的原因,我的 ConsultantId 为 0 并且当我的 ViewModel 中的 ProfessionalEducation 为空时,在我的控制器中回发到 HttpPost Create 方法时。
这应该很基本,我忘记了什么?
提前致谢!!