-One is drawing List Box to select record-"SelectionVer1.cshtml"
-2nd is drawing HTML Table with checkbox to select a record- "SelectionVer2.cshtml"
(Both are using same model and same action method of a controller)
Now I need to dynamically render this based on the logged in User. If user have roll1 then SelectionVer2.cshtml will be rendered by same controller otherwise SelectionVer1.cshtml will be rendered by same controller.
How this can be achieved?
AnilKr
Marked as answer by akfkmupiwu on Apr 12, 2012 12:50 PM
The user-view association is stored in database i.e. this is dynamic.
There is possibility of adding new views as view library is another project.
I can not hard code User Name and Views, as the association may be changed, i need approach like Dependency Injection, we are using this for injecting Model, but I am stuck for Views.
AnilKr
Marked as answer by akfkmupiwu on Apr 12, 2012 12:49 PM
Thanks for the concept, we have implemented in below manner.
public ActionResult Index()
{
bm = bd.GetBMCs(5);
//using static class VersionControl to get components for user
return View(VersionControl.GetComponent(HttpContext.User.Identity.Name, ComponentName.RangePlanConfigSelection), null, bm);
}
Its working !
AnilKr
Marked as answer by akfkmupiwu on Apr 12, 2012 12:48 PM
akfkmupiwu
Member
204 Points
78 Posts
Rendering views dynamically
Apr 12, 2012 10:47 AM|LINK
I have two views
-One is drawing List Box to select record-"SelectionVer1.cshtml"
-2nd is drawing HTML Table with checkbox to select a record- "SelectionVer2.cshtml"
(Both are using same model and same action method of a controller)
Now I need to dynamically render this based on the logged in User. If user have roll1 then SelectionVer2.cshtml will be rendered by same controller otherwise SelectionVer1.cshtml will be rendered by same controller.
How this can be achieved?
AlexMgn
Member
102 Points
24 Posts
Re: Rendering views dynamically
Apr 12, 2012 10:59 AM|LINK
try use something like this
<html> ....... @if(Request.User.Identity.IsInRole("Admin")) { @Html.RenderPartial("View1",Model); } else { @Html.RenderPartial("View2",Model); } ..... </html>raduenuca
All-Star
24675 Points
4250 Posts
Re: Rendering views dynamically
Apr 12, 2012 10:59 AM|LINK
public ActionResult Index() { if( HttpContext.User.Identity.Name == 'User1') { return View("View1"); } if( HttpContext.User.Identity.Name == 'User2') { return View("View2"); } //return a default view return View(); }Radu Enuca | Blog
akfkmupiwu
Member
204 Points
78 Posts
Re: Rendering views dynamically
Apr 12, 2012 11:12 AM|LINK
The user-view association is stored in database i.e. this is dynamic.
There is possibility of adding new views as view library is another project.
I can not hard code User Name and Views, as the association may be changed, i need approach like Dependency Injection, we are using this for injecting Model, but I am stuck for Views.
AlexMgn
Member
102 Points
24 Posts
Re: Rendering views dynamically
Apr 12, 2012 11:16 AM|LINK
So, my suggestion based on Roles. It means you don't need to hardcode username.
If your Views will change you can code something like version control that will return actual associated Views names.
akfkmupiwu
Member
204 Points
78 Posts
Re: Rendering views dynamically
Apr 12, 2012 12:48 PM|LINK
Thanks for the concept, we have implemented in below manner.
public ActionResult Index() { bm = bd.GetBMCs(5); //using static class VersionControl to get components for user return View(VersionControl.GetComponent(HttpContext.User.Identity.Name, ComponentName.RangePlanConfigSelection), null, bm); }Its working !