gpt4 book ai didi

c# - 在 mvc 中一次保存多个 View 模型数据

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:04 25 4
gpt4 key购买 nike

我有 5 个类:

1) EmployeeMainTable.cs (Employeeid 是主键,employee id 是其余类的外键)

2) EmployeeFamilyTable.cs

3) previousEmployeement.cs

4) 员工地址.cs

5) employeeEducation.cs

每个类都包含各自的属性,在此处输入代码

现在我想为每个类做些什么,我创建了 5 个 View 来输入数据,每个 View 都包含提交按钮以转到下一个 View

我的问题是,当我在最后一个 View 中单击完成按钮时,我想立即保存所有模型。

请在使用 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:表达式树不能包含动态操作

出现错误

@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:

最佳答案

您应该适本地将以前的 View 模型传递给“下一个” View ,这样当单击“完成”按钮时,您可以检索所有模型(在 Controller 中)以在最后创建。 (不要忘记将您的模型放在帖子中)

更新 1:

为什么 Controller 需要 GET 和 POST 方法?

USPDATE2:例子

**案例 1:如果您有多个 View 模型**

Controller

[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 ?
}

观看次数第一步:

@model FirstStep
@using(Html.BeginForm("SecondStep", "controllerName", FormMethod.Post))
{
@Html.EditorFor(model => model.Value)
<input type="submit" value="Next"/>
}

第二步:

@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"/>
}

第三步:

@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"/>
}

第四步:

@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"/>
}

第五步:

@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:单 View 模型小心:为了使它起作用,您将需要(取决于模型的复杂性)模型的 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 ?
}

...

观看次数

第一步:

@model Model
@using(Html.BeginForm("SecondStep", "controllerName", FormMethod.Post))
{
@Html.EditorFor(model => model.Step1Value)
<input type="submit" value="Next"/>
}

第二步:

@model Model
@using(Html.BeginForm("ThirdStep", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => model.Step1Value)
@Html.EditorFor(model => model.Step2Value)
<input type="submit" value="Next"/>
}

第三步:

@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"/>
}

关于c# - 在 mvc 中一次保存多个 View 模型数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26137695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com