2013-07-12 23 views
8

Tôi đang xem Bộ phận chứa thông tin bộ phận. Tôi có Page view của nhân viên. trong quan điểm của nhân viên tôi đang sử dụng một phần của bộ phận để hiển thị bộ phận nhân viên. Mô hình nhân viên của tôi như sau:cách duy trì dữ liệu mô hình xem một phần trong khi đăng lại trong asp.net mvc

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

} 

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

Tôi đã gửi nút trên chế độ xem trang của nhân viên. Khi tôi gửi chế độ xem nhân viên, tôi nhận được đối tượng Sở là rỗng. Bạn có thể đề nghị tôi làm thế nào tôi có thể nhận được mô hình Bộ trẻ em trong khi đăng lại. Coltroller đang

[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); 
     } 

Lần

cục

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

nhân viên

@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") 
} 
+8

các cử tri xuống có thể vui lòng giải thích tại sao câu hỏi này đã được bình chọn xuống. Đó là một câu hỏi hay. Đừng chỉ bỏ phiếu và bỏ chạy. Hãy để các poster biết tại sao. –

Trả lời

4

Hãy thử tìm kiếm đầu tiên. Câu hỏi này đã được hỏi nhiều lần trước đây.

Dưới đây là một cách để làm điều đó:

mvc partial view post

Tóm tắt: quấn từng phần trong nhiều thẻ hình thức mỗi với riêng của họ nút gửi.

Nhưng lần này có vẻ giống như những gì bạn đang sau:

Post a form with multiple partial views

Sử dụng editortemplates cho điều này thay vì partials.

Sự cố bạn gặp phải là khi hộp văn bản NameName của bạn không được đặt tên chính xác để trình điều khiển của bạn đọc nó. POST của bạn sẽ là EmployeeName=Prashant&DepartmentName=Phy do đó Departmentnull, do đó có lỗi.

4

Hãy thử điều này,

Ngoài ra, bạn đã thay đổi tên tổ chức Mô hình bộ. Và vị trí của Chế độ xem trong Shared/EditorTemplates.

XEM:

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

CONTROLLER:

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

      return View(e); 
     } 
Các vấn đề liên quan