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:
Master View (MultiModelView.cshtml) ://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; } } }
@model PartialVsAction.Models.ModelBjavascript:void(0) @{ ViewBag.Title = "MultiModelView"; } <h2> MultiModelView</h2> <div> @Html.Partial("PersonnelPartial",Model) </div> <div> @Html.Partial("ProffesionalPartial",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.Model.ProfessionalDetails ) </div>
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);Result:return PartialView(objA); }
Objective Achieved :)
Conclusion:
Case 1: Use
Case 2: Use
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.
Thanks ,It was very helpful
ReplyDeleteThis was really helpful, Pratap!
ReplyDeleteNice post and nice image enlarging.
ReplyDeleteBTW correct: "This can be achieved buy" (by)
Thanks. Correction done.
DeleteVery nice article. I was exactly looking for this.
ReplyDeleteGreat Post.... Thanks a lot
ReplyDeleteThanks, now I understand why and how to use the optional object model parameter..!
ReplyDeleteI was very much helpful for me Thanks
ReplyDeleteI was very much helpful for me Thanks
ReplyDeleteCan u pls tell how enlarging of img is hapening..
ReplyDeleteNice article! thanks.
ReplyDeleteGreat article
ReplyDeleteNice
ReplyDeleteHi All!
ReplyDeletePages 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!