I am new to MVC and using MVC3 with Visual Studio 2010 Pro.
My question, how I can pass a parameter to the layout page from a hyper link of another view?
Scenario
My Project has
1. View : Student (using Layout1 as Layout file)
2. View : Public (using Layout2 as Layout file).
3. PartialView : StudentSummary (calling using @RenderAction in Layout2, with a Controller Action with the help of attribute [childActionOnly] and this child action recieves a parameter).
I am trying to pass the parameter from the Student page to the partial view. So when I click on the hyperlink, it calls the Public view and then the public view renders the partial view.
Since the Partial view is bieng called inside the layout2 I need to pass the parameter from the Student page to Layout2, so that I can use the below code to call the partial view inside Layout2
I really appreciate your quick reply. I apologize that I could not reply right away.
It was a great knowledge about the view model, which we can implement when we have information from multiple sources to be rendered on the view.
Would you be able to help me to know if it is a good practice to implement the ViewModel in my scenario?
I tried implementing the view model as below.
I need to pass only the StudentId to Layout page, so created a ViewModel of type StudentInfo which will have the StudentId as a property.
and assigned the type of Layout view as StudentInfo on the top of the Layout page using @model School.ViewModels.StudentInfo.
Since it is a strongly types LAYOUT PAGE and it will be used as a parent for many other views, it throws error, when I tried to access a couple of other views, which say, "the passed type and the type of the view are different".
It would be great, if you can shed some light on the mistakes what I do here and to correct me.
Best Regards, Abdulla.
Don't forget to "Mark As Answer" if a post helps you.
I really dont support having a typed layout page. But since you have a scenario which requires it, you can try creating a base viewmodel and inheriting your other viewmodels from it. Now type your layout with the base viewmodel and your view with the view
specific viewmodels.
Cheers,
Gopakumar
| Please click “Mark as Answer” on the post(s) if it helps |
May I know why using a typed Layout page is not a good practice? Since I need only one parameter in the layout page, Do you think, it is better to create a global variable Public static int StudentID, and call it every where? or do you beleive, using session
is better instead?
Don't forget to "Mark As Answer" if a post helps you.
There is another feature in MVC called ViewBag. This is of type dynamic (.Net 4 feature). You can put any data into viewbag and use it in you view as below
Controller Action
ViewBag.Data="Value";
View
<div>@ViewBag.Data</div>
Note: View bag values will be available only in the view. The subsequest request the data will be lost.
You can use another persistance dictionary called TempData, as below
Controller
TempData["Data"]=value;
View
<div>@TempData["Data"]</div>
Note: Temp data will be available in the view as well as in the subsequest request too. The second request tempdata is lost.
You can choose what to use.
Cheers,
Gopakumar
| Please click “Mark as Answer” on the post(s) if it helps |
I tried using ViewBag.StudentId, in the controller, but it is not available in the layout page, it is available only in the Partial View.
This is the first time I hear about TempData[Key] (thank you for that), and I tried this too, but, this value is also not available in the layout page.
Any other suggestion?
Don't forget to "Mark As Answer" if a post helps you.
Ah! I dont understand why both of these are not available in layout. I will give it a try and let you know the problem
Meanwhile you can try using Sections, if it satisfies your requirement, it is used as below
Layout
@RenderSection("SectionName")
View
@section SectionName{
//All your HTML code goes here
}
Here what happens is that the render section in you layout page will be replaced by the @section html code defined in your views. So you will have to just pass the values to your view and not to your layout page. Also we will avoid using typed layout page.
abdu292
Participant
1553 Points
371 Posts
Passing paramter to Layout from view
May 03, 2012 09:08 AM|LINK
Hello,
I am new to MVC and using MVC3 with Visual Studio 2010 Pro.
My question, how I can pass a parameter to the layout page from a hyper link of another view?
Scenario
My Project has
1. View : Student (using Layout1 as Layout file)
2. View : Public (using Layout2 as Layout file).
3. PartialView : StudentSummary (calling using @RenderAction in Layout2, with a Controller Action with the help of attribute [childActionOnly] and this child action recieves a parameter).
I am trying to pass the parameter from the Student page to the partial view. So when I click on the hyperlink, it calls the Public view and then the public view renders the partial view.
Since the Partial view is bieng called inside the layout2 I need to pass the parameter from the Student page to Layout2, so that I can use the below code to call the partial view inside Layout2
@RenderAction("StudentSummary","Student",new { id=iRecievedIdfromStudent })
Can any one help me how to pass the parameter to Layout page?
Thank you in advance,
Abdulla.
With best regards,
DarrellNorto...
All-Star
86555 Points
9624 Posts
Moderator
MVP
Re: Passing paramter to Layout from view
May 03, 2012 09:11 AM|LINK
Create a ViewModel and use a strongly typed View.
Follow this blog post for Razor: http://www.rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.net-mvc-applications
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
abdu292
Participant
1553 Points
371 Posts
Re: Passing paramter to Layout from view
May 04, 2012 04:20 AM|LINK
Hi DarellNorton,
I really appreciate your quick reply. I apologize that I could not reply right away.
It was a great knowledge about the view model, which we can implement when we have information from multiple sources to be rendered on the view.
Would you be able to help me to know if it is a good practice to implement the ViewModel in my scenario?
I tried implementing the view model as below.
I need to pass only the StudentId to Layout page, so created a ViewModel of type StudentInfo which will have the StudentId as a property.
and assigned the type of Layout view as StudentInfo on the top of the Layout page using @model School.ViewModels.StudentInfo.
Since it is a strongly types LAYOUT PAGE and it will be used as a parent for many other views, it throws error, when I tried to access a couple of other views, which say, "the passed type and the type of the view are different".
It would be great, if you can shed some light on the mistakes what I do here and to correct me.
Best Regards, Abdulla.
With best regards,
gopakumar.r
Participant
959 Points
193 Posts
Re: Passing paramter to Layout from view
May 04, 2012 04:32 AM|LINK
I really dont support having a typed layout page. But since you have a scenario which requires it, you can try creating a base viewmodel and inheriting your other viewmodels from it. Now type your layout with the base viewmodel and your view with the view specific viewmodels.
Gopakumar
| Please click “Mark as Answer” on the post(s) if it helps |
abdu292
Participant
1553 Points
371 Posts
Re: Passing paramter to Layout from view
May 04, 2012 04:38 AM|LINK
Hi GopaKumar,
Thank you for your response.
May I know why using a typed Layout page is not a good practice? Since I need only one parameter in the layout page, Do you think, it is better to create a global variable Public static int StudentID, and call it every where? or do you beleive, using session is better instead?
With best regards,
gopakumar.r
Participant
959 Points
193 Posts
Re: Passing paramter to Layout from view
May 04, 2012 04:55 AM|LINK
There is another feature in MVC called ViewBag. This is of type dynamic (.Net 4 feature). You can put any data into viewbag and use it in you view as below
Controller Action
View
Note: View bag values will be available only in the view. The subsequest request the data will be lost.
You can use another persistance dictionary called TempData, as below
Controller
View
Note: Temp data will be available in the view as well as in the subsequest request too. The second request tempdata is lost.
You can choose what to use.
Gopakumar
| Please click “Mark as Answer” on the post(s) if it helps |
abdu292
Participant
1553 Points
371 Posts
Re: Passing paramter to Layout from view
May 04, 2012 05:13 AM|LINK
I tried using ViewBag.StudentId, in the controller, but it is not available in the layout page, it is available only in the Partial View.
This is the first time I hear about TempData[Key] (thank you for that), and I tried this too, but, this value is also not available in the layout page.
Any other suggestion?
With best regards,
gopakumar.r
Participant
959 Points
193 Posts
Re: Passing paramter to Layout from view
May 04, 2012 06:26 AM|LINK
Ah! I dont understand why both of these are not available in layout. I will give it a try and let you know the problem
Meanwhile you can try using Sections, if it satisfies your requirement, it is used as below
Layout
@RenderSection("SectionName")View
@section SectionName{ //All your HTML code goes here }Here what happens is that the render section in you layout page will be replaced by the @section html code defined in your views. So you will have to just pass the values to your view and not to your layout page. Also we will avoid using typed layout page.
You can read more about Section in the below link
http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx
Let me know if this one satisfy your requirement.
Gopakumar
| Please click “Mark as Answer” on the post(s) if it helps |
abdu292
Participant
1553 Points
371 Posts
Re: Passing paramter to Layout from view
May 04, 2012 09:09 AM|LINK
I tried using section as mentioned.
I am wondering if it allows us to create the content dynamically instead of static content, since I've a child action associated with it.
My Partial view is getting created in one of the controls, and this is what will be shown in the layout page as partial view.
Do you think, we can use a child controller/controller action for section as well?
The link you provided is a great resource for section example, but it explains only about static content and no action method associated with it.
Any help will be really appreciated.
With best regards,
abdu292
Participant
1553 Points
371 Posts
Re: Passing paramter to Layout from view
May 06, 2012 01:49 PM|LINK
Hi gopakumar,
Did you get some time to check, ViewBag and TempData and its accessibility in the layout page?
Regards,
With best regards,