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

标签 c# asp.net-mvc-4

我有 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/

相关文章:

c# - 使用正则表达式列表查找匹配的目录

c# - 根据 JSON 对象属性对 jquery DataTables 进行排序 - 使用正交数据

c# - 在没有 MVC 的纯 ASP.NET Web API 服务中,我们是否仍然需要 Global.asax 中的 AreaRegistration.RegisterAllAreas()

c# - 使用 ContentResult 类型对 C# 进行单元测试

c# - Controller 生成的 <ul> 和 <li> 标签作为文字文本加载

c# - 是否有一些正确的方法可以为 IEnumerable 模型字段添加模型错误以突出显示 View 中的特定输入但不是全部?

c# - 将列表从 jQuery 传递到服务

c# - 如何在 Windows 窗体中显示组合框所选项目的全文?

c# - 在没有 BOM 的情况下在 c# utf-8 中编码文本

c# - ClaimsAuthenticationManager.Authenticate 永远不会被调用