如何在回发期间在asp.net mvc中保留部分视图模型数据

前端之家收集整理的这篇文章主要介绍了如何在回发期间在asp.net mvc中保留部分视图模型数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在部分查看包含部门信息的部门.我有员工的页面视图.在员工视图中我使用部门视图来显示员工部门.我的员工模型如下
class Employee
{
  public string EmployeeName{get;set};
  public Department EmployeeName{get;set};

}

class Department
{
  public string DepartmentName{get;set};
}

我在员工页面视图上提交了按钮.
当我提交员工视图时,我将Department对象视为null.
你能否告诉我如何在回发期间获得儿童部门模型.
Coltroller代码

[HttpGet]
        public ActionResult Employee2()
        {
            Employee e = new Employee();
            e.EmployeeName = "Prashant";
            e.Department = new Department() { DepartmentName = "Phy" };

            return View(e);
        }

        [HttpPost]
        public ActionResult Employee2(Employee e)
        {

            return View(e);
        }

查看

部门

@model MvcApplication2.Models.Department

<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

    <fieldset>
        <legend>Department</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.DepartmentName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DepartmentName)
            @Html.ValidationMessageFor(model => model.DepartmentName)
        </div>


    </fieldset>

<div>
    @Html.ActionLink("Back to List","Index")
</div>

雇员

@model MvcApplication2.Models.Employee
@{
    ViewBag.Title = "Employee";
}
<h2>
    Employee</h2>
@using (Html.BeginForm("Index","Home"))
{

    <fieldset>
        <legend>Employee</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.EmployeeName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.EmployeeName)
            @Html.ValidationMessageFor(model => model.EmployeeName)
        </div>
        @Html.Partial("Department",Model.Department)
        <p>
            <input type="submit" value="EmployeeSave" />
        </p>
    </fieldset>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

解决方法

首先尝试搜索.之前已经多次询问过这个问题.

这是一种方法

mvc partial view post

摘要:使用自己的提交按钮将每个部分包装在多个表单标记中.

但是这个看起来更像是你所追求的:

Post a form with multiple partial views

使用editortemplates代替partials.

您遇到的问题是,当您的DepartmentName文本框未正确命名时,您的控制器无法读取它.你的POST将是EmployeeName = Prashant& DepartmentName = Phy因此Department为null,因此出错.

原文链接:https://www.f2er.com/aspnet/248616.html

猜你在找的asp.Net相关文章