Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to persist partial view model data during postback in asp.net mvc
    primarykey
    data
    text
    <p>I am having partial view Department containing department information. I have Page view of Employee. in Employee view i am using partial view of department to show employees department. My employee model is as below</p> <pre><code>class Employee { public string EmployeeName{get;set}; public Department EmployeeName{get;set}; } class Department { public string DepartmentName{get;set}; } </code></pre> <p>I have submit button on employee page view. When i submit the employee view I am getting Department object as null. Could you please suggest me how i can get child Department model during postback. Coltroller code</p> <pre><code>[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); } </code></pre> <p>Views</p> <p>Department</p> <pre><code>@model MvcApplication2.Models.Department &lt;script src="~/Scripts/jquery-1.7.1.min.js"&gt;&lt;/script&gt; &lt;script src="~/Scripts/jquery.validate.min.js"&gt;&lt;/script&gt; &lt;script src="~/Scripts/jquery.validate.unobtrusive.min.js"&gt;&lt;/script&gt; &lt;fieldset&gt; &lt;legend&gt;Department&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.DepartmentName) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.DepartmentName) @Html.ValidationMessageFor(model =&gt; model.DepartmentName) &lt;/div&gt; &lt;/fieldset&gt; &lt;div&gt; @Html.ActionLink("Back to List", "Index") &lt;/div&gt; </code></pre> <p>Employee</p> <pre><code>@model MvcApplication2.Models.Employee @{ ViewBag.Title = "Employee"; } &lt;h2&gt; Employee&lt;/h2&gt; @using (Html.BeginForm("Index","Home")) { &lt;fieldset&gt; &lt;legend&gt;Employee&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.EmployeeName) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.EmployeeName) @Html.ValidationMessageFor(model =&gt; model.EmployeeName) &lt;/div&gt; @Html.Partial("Department", Model.Department) &lt;p&gt; &lt;input type="submit" value="EmployeeSave" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } @section Scripts { @Scripts.Render("~/bundles/jqueryval") } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload