我有5个班级:
1)EmployeeMainTable.cs (Employeeid是主键,员工ID是其他类的外键)
2)EmployeeFamilyTable.cs
3)previousEmployeement.cs
4)employeeaddress.cs
5)employeeEducation.cs
每个类都包含相应的属性输入代码
现在我要为每个班级做些什么我创建了5个视图来输入数据,每个视图都包含提交按钮以转到下一个视图
我的问题是,当我在上一个视图中点击完成按钮时,我想立即保存所有模型。
使用mvc 4时请理解问题
我试过以下结果:
@model SecondStep
@using(Html.BeginForm("ThirdStep", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => ViewBag.FirstStepModel.Value)
@Html.EditorFor(model => model.Value)
<input type="submit" value="Next"/>
}
CS1963: An expression tree may not contain a dynamic operation
中出错
@Html.HiddenFor(model => ViewBag.FirstStepModel.Value)
Line 13: {
Line 14:
Line 15: @Html.HiddenFor(model=>ViewBag.FirstStepModel) this line giving error
Line 16: @Html.EditorFor(model => model.SecondStepProp)
Line 17:
答案 0 :(得分:1)
您应该将以前的视图模型多次传递给&#34; next&#34;查看,所以当&#34;完成&#34;单击按钮,您可以检索所有模型(在控制器中)以在最后创建。 (别忘了把你的模型放在帖子中)
<强> UPDATE1:强>
为什么在控制器上需要GET和POST方法?
USPDATE2:示例
**案例1:如果您有多个视图模型**
<强>控制器强>
[HttpGet]
public ActionResult FirstStep()
{
return View(new FirstStepModel());
}
[HttpPost]
public ActionResult SecondStep(FirstStepModel firstStepModel)
{
ViewBag.FirstStepModel = firstStepModel;
return View(new SecondStepModel());
}
[HttpPost]
public ActionResult ThridStep(FirstStepModel firstStepModel, SecondStepModel secondStepModel)
{
ViewBag.FirstStepModel = firstStepModel;
ViewBag.SecondStepModel = secondStepModel;
return View(new ThirdStepModel());
}
[HttpPost]
public ActionResult FourthStep(FirstStepModel firstStepModel, SecondStepModel secondStepModel, ThirdStepModel thirdStepModel)
{
ViewBag.FirstStepModel = firstStepModel;
ViewBag.SecondStepModel = secondStepModel;
ViewBag.ThirdStepModel = thirdStepModel;
return View(new FourthStepModel()));
}
[HttpPost]
public ActionResult FifthStep(FirstStepModel firstStepModel, SecondStepModel secondStepModel, ThirdStepModel thirdStepModel, FourthStepModel fourthStepModel)
{
ViewBag.FirstStepModel = firstStepModel;
ViewBag.SecondStepModel = secondStepModel;
ViewBag.ThirdStepModel = thirdStepModel;
ViewBag.FourthStepModel = fourthStepModel;
return View(new FinalStepModel());
}
[HttpPost]
public ActionRestul Finish(FirstStepModel firstStepModel, SecondStepModel secondStepModel, ThirdStepModel thirdStepModel, FourthStepModel fourthStepModel, FifthStepModel fifthStepModel)
{
// DB Save ?
}
<强>视图强> FirstStep:
@model FirstStep
@using(Html.BeginForm("SecondStep", "controllerName", FormMethod.Post))
{
@Html.EditorFor(model => model.Value)
<input type="submit" value="Next"/>
}
SecondStep:
@model SecondStep
@using(Html.BeginForm("ThirdStep", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => ViewBag.FirstStepModel.Value)
@Html.EditorFor(model => model.Value)
<input type="submit" value="Next"/>
}
ThirdStep:
@model ThirdStep
@using(Html.BeginForm("FourthStep", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => ViewBag.FirstStepModel.Value)
@Html.HiddenFor(model => ViewBag.SecondStepModel.Value)
@Html.EditorFor(model => model.Value)
<input type="submit" value="Next"/>
}
FourthStep:
@model FourthStep
@using(Html.BeginForm("FifthStep", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => ViewBag.FirstStepModel.Value)
@Html.HiddenFor(model => ViewBag.SecondStepModel.Value)
@Html.HiddenFor(model => ViewBag.ThirdStepModel.Value)
@Html.EditorFor(model => model.Value)
<input type="submit" value="Next"/>
}
FifthStep:
@model FifthStep
@using(Html.BeginForm("Finish", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => ViewBag.FirstStepModel.Value)
@Html.HiddenFor(model => ViewBag.SecondStepModel.Value)
@Html.HiddenFor(model => ViewBag.ThirdStepModel.Value)
@Html.HiddenFor(model => ViewBag.FifthStepModel.Value)
@Html.EditorFor(model => model.Value)
<input type="submit" value="Next"/>
}
案例2:单一视图模型 小心:要实现这一点,您需要(取决于您的模型的复杂性)模型的ModelBinder
[HttpGet]
public ActionResult FirstStep()
{
return View("FirstStep", new Model());
}
[HttpPost]
public ActionResult SecondStep(Model model)
{
return View("SecondStep", model);
}
[HttpPost]
public ActionResult ThridStep(Model model)
{
return View("ThirdStep", model);
}
[HttpPost]
public ActionResult Finish(Model model)
{
// DB Save ?
}
...
<强>视图强>
FirstStep:
@model Model
@using(Html.BeginForm("SecondStep", "controllerName", FormMethod.Post))
{
@Html.EditorFor(model => model.Step1Value)
<input type="submit" value="Next"/>
}
SecondStep:
@model Model
@using(Html.BeginForm("ThirdStep", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => model.Step1Value)
@Html.EditorFor(model => model.Step2Value)
<input type="submit" value="Next"/>
}
ThirdStep:
@model Model
@using(Html.BeginForm("Finish", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => model.Step1Value)
@Html.HiddenFor(model => model.Step2Value)
@Html.EditorFor(model => model.Step3Value)
<input type="submit" value="Next"/>
}