8

我正在部分查看包含部门信息的部门。我有员工的页面视图。在员工视图中,我使用部门的部分视图来显示员工部门。我的员工模型如下

class Employee
{
  public string EmployeeName{get;set};
  public Department EmployeeName{get;set};

}

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

我在员工页面视图上有提交按钮。当我提交员工视图时,我将部门对象设为空。您能否建议我在回发期间如何获得子部门模型。管理员代码

[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")
}
4

2 回答 2

5

先尝试搜索。这个问题之前已经被问过很多次了。

这是一种方法:

mvc局部视图帖子

摘要:将每个部分包装在多个表单标签中,每个标签都有自己的提交按钮。

但这似乎更像你所追求的:

发布具有多个部分视图的表单

为此使用编辑器模板而不是部分。

您遇到的问题是,当您的 DepartmentName 文本框没有正确命名以供控制器读取时。因此,您POST将是,因此是错误。EmployeeName=Prashant&DepartmentName=PhyDepartmentnull

于 2013-07-12T05:46:25.810 回答
4

试试这个,

您还更改了部门模型实体名称。以及共享/编辑器模板中的部门视图位置。

看法:

@using (Html.BeginForm("Employee", "Content"))
{
@Html.EditorFor(model => model.dptEmployeeName.DepartmentName)
    <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.EditorFor(model => model.dptEmployeeName.DepartmentName) //This is partial view

        <p>
            <input type="submit" value="EmployeeSave" />
        </p>
    </fieldset>
}

控制器:

[HttpPost]
        public ActionResult Employee(Employee e,FormCollection frm)
        {
            var asd = frm["dptEmployeeName.DepartmentName"];

            return View(e);
        }
于 2013-07-12T06:39:55.167 回答