Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to persist partial view model data during postback in asp.net mvc

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

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

}

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

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

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

Views

Department

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

Employee

@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")
}
like image 263
Prashant Avatar asked Jul 12 '13 04:07

Prashant


People also ask

Can we return partial view in MVC?

In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.

How do you pass a partial view model?

To create a partial view, right click on Shared folder -> select Add -> click on View.. Note: If the partial view will be shared with multiple views, then create it in the Shared folder; otherwise you can create the partial view in the same folder where it is going to be used.

Where are partial views stored?

For Razor, a partial view is a . cshtml file located under the Views project folder, in a specific controller folder or in the Shared folder. As a developer, you identify partial views by name and can reference them in a Razor view file in either of two ways: using the Html. Partial method or Html.

Can we return partial view from an action?

To return a Partial view from the controller action method, we can write return type as PartialViewResult and return using PartialView method.


2 Answers

Try searching first. This question has been asked many times before.

Here is one way to do it:

mvc partial view post

Summary: wrap each partial in multiple form tags each with their own submit button.

But this one seems more like what you're after:

Post a form with multiple partial views

Use editortemplates for this instead of partials.

The issue you're having is that when your DepartmentName textbox isn't being named correctly for your controller to read it. Your POST would be EmployeeName=Prashant&DepartmentName=Phy therefore Department is null, hence the error.

like image 114
mnsr Avatar answered Sep 22 '22 13:09

mnsr


Try this,

Also you changed the Department Model entity name. And Department view location in Shared/EditorTemplates.

VIEW:

@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);
        }
like image 28
Jeet Bhatt Avatar answered Sep 23 '22 13:09

Jeet Bhatt