-->

26/11/2011

@Html.Partial() Vs @Html.Action() - MVC Razor


In earlier post we have seen how to render different partial views (each using different model).
By which we displayed both Personal and Professional details of an employee as below:
Now lets look at other aspect.
Objective: I wanna display Professional details of an employee for both 2010 and 2011.
This means Using the same View and Same model but i need different data.
Lets see how this gonna be achieved.
Step 1:  Current Infrastructure we have.
Models for both Personal and Professional details:
//Professional Details
namespace PartialVsAction.Models
{
    public class ModelA
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }        
        public string Comapny { get; set; }
        public int FisacalYear { get; set; }
    }
}
//Personal Details
namespace PartialVsAction.Models
{
    public class ModelB
    {
        public ModelA ProfessionalDetails;
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }        
        public string Address { get; set; }
        public string MobileNo { get; set; }
        public string Age { get; set; }
        public int FiscalYear { get; set; }
    }
}
Master View (MultiModelView.cshtml) :
@model PartialVsAction.Models.ModelBjavascript:void(0)
@{
    ViewBag.Title = "MultiModelView";
}
<h2>
MultiModelView</h2>
<div>
@Html.Partial("PersonnelPartial",Model)
</div>
<div>
@Html.Partial("ProffesionalPartial",Model.ProfessionalDetails)
</div>
If you observe the both Models and Master View,  there is a property in Personal model which is of type Professional model. and the same was assigned to Partial view in Master View code.

Step 2 : We need two Partial views displaying Professional details for both 2010 and 2011.
So firstly we need to add another Partial view of type "ProfesionalPartial", just like second one.


Step 3: So now the code of Master View will be like below:
@model PartialVsAction.Models.ModelB
@{
    ViewBag.Title = "MultiModelView";
}
<h2>MultiModelView</h2>
<div>
@Html.Partial("PersonnelPartial",Model)
</div>
<div>
@Html.Partial("ProffesionalPartial",Model.ProfessionalDetails)
</div>
<div>
@Html.Partial("ProffesionalPartial",Model.ProfessionalDetails)
</div>
Now, the result will look like:
Oops, we got two professional detail views but with same data.
Reason, we have only one property added in the model. so, using the ModelA with data for 2011 will not work in this case.

Step 4:So we need to hit another action method in order to use same ModelA but with 2010 data.
This can be achieved by @Html.Action() helper method.
All we need to do are two things.
         i. Try using @Html.Action() instead of @Html.Partial()
        ii. Create a new Action method in Controller sending the same ModelA with whatever data we want.

Step 5: Now our Master View will be like this:
@model PartialVsAction.Models.ModelB
@{
    ViewBag.Title = "MultiModelView";
}
<h2>MultiModelView</h2>
<div>
@Html.Partial("PersonnelPartial",Model)
</div>
<div>
@Html.Partial("ProffesionalPartial",Model.ProfessionalDetails)
</div>
<div>
@Html.Action("ProffesionalPartial", "Home")</div>

Step 6: Add a new Action method in Controller.
public ActionResult ProffesionalPartial()
{
   EmployeeService svc = new EmployeeService();
   ModelA objA = new ModelA();
   objA = svc.GetEmployeeProffessionalDetails(111, 2010);

   return PartialView(objA);
}
Result:
 Objective Achieved :)

Conclusion: 
Case 1: Use @Html.Partial() method if you have Different Partial Views with different Models.
Case 2: Use @Html.Action() method if you have one Partial View with one Model, but different data.

Caution: You can happily use Action() in case of #Case1 as well but why to hit controller twice when you can get both the models in one hit as described in earlier post.
Code:
Click Here
Is it helpful for you? Kindly let me know your comments / Questions.

14 comments:

  1. Thanks ,It was very helpful

    ReplyDelete
  2. This was really helpful, Pratap!

    ReplyDelete
  3. Nice post and nice image enlarging.

    BTW correct: "This can be achieved buy" (by)

    ReplyDelete
  4. Very nice article. I was exactly looking for this.

    ReplyDelete
  5. Great Post.... Thanks a lot

    ReplyDelete
  6. Thanks, now I understand why and how to use the optional object model parameter..!

    ReplyDelete
  7. I was very much helpful for me Thanks

    ReplyDelete
  8. I was very much helpful for me Thanks

    ReplyDelete
  9. Can u pls tell how enlarging of img is hapening..

    ReplyDelete
  10. Hi All!
    Pages Controller
    public ActionResult chuyentrang()

    {

    var data = "hello";
    // ViewBag.Message=data;// i can get value to pages view
    return View("_chuyentrang", new { data});// i can't get value to pages views

    }

    Pages Views
    @Model
    @ViewBag.Message

    ASP beginner I encountered this problem but could not get the value data to page views.! Please help me!

    ReplyDelete