The question might be a likely duplicate of
Can't access ViewBag in a partial view in ASP.NET MVC3,
but it does not seem to categorically negate the possibility.
The issue is to Access ViewBag Data in partial View which is being called from within the View and is being passed a model e.g.
#Html.Partial("name", object);
The the Action method for the main View from which this partial View is being called has a Viewbag property .. That Viewbag does not seem to be accessed with the Partial view.
Any help appreciated..
Thank you for the time..
have found the solution in the following post
MVC3 - Passing data beyond the model to Partial view
Regards.
Related
I'm working on some forms, and often I can reuse the same amount and combination of fields together, is it possible to group them all as a template and then call it from the page?
For example:
2 radio buttons with labels and 2 texboxes under.
How is it called so i can do a proper research?
You can use Partial View, its like a reusable component.
These links will elaborate more:
http://www.devcurry.com/2012/04/partial-views-in-aspnet-mvc-3.html
http://www.dotnet-tricks.com/Tutorial/mvc/2IKW160912-Partial-View-in-Asp.net-MVC3-Razor.html
MVC3 Partial Views
Create a Partial View and keep your markup which you want to reuse in that. You can use these Partial views in other views as needed then.
You can use the Html.Partial helper method to call the partial views in other views.
#Html.Partial("RecentItems")
You can also pass some model to your partial view as well
#Html.Partial("RecentItems", Model.RecentItems)
Assuming Your partial view is strongly typed to a class which is of the same type as the type of Model.RecentItems, of the caller view.
I have created a base viewmodel that all of my view models inherit from. That part is easy.
All views are bound to a viewmodel (all are inherited from the base view model)
Within the OnActionExecuted method I insert a true/false value onto a property within the baseviewmodel depending on some conditions.
From the view side of things. I have a single layout page that some how I want to be able to read the property's value and render a different partial view based on the value.
Is this possible? I dont want to have to add the code to each of the views but I don't think I should be binding the layout to my baseviewmodel either.
If I can stay away from inserting the value into the valuebag that would be great as I need to be able to access these values anywhere in the application via strongly typed names.
what you want probably isnt possible because as you call a view from the controller then first the code inside that view is executed and then the layouts code is executed
to achieve what you are doing you can do 2 things
1. make the logic inside the controller itself and then render the correct view
2.call the layout from the controller giving it the name of the partial view in some property of the model or in the viewbag
Not sure I quite follow the use case, but rather than trying to render out a partial view, have you thought about nesting your layout pages.
I think you should be able to override the layout in the onactionexecuted, so you can set the layout dependant on the bool and that layout will render only the correct option.
Look here for an example:
Nested layout pages with Razor
HTH
Si
I am aware of this question, but the original poster accepted a solution that didn't involve nesting. I definitely want to nest partial views (unless, of course, there's a better way.)
I have a page that can Ajax-load one of several partial views, depending on the user's actions in the main view. (The views are partial because my understanding is that if you want to load significant additional content from an Ajax call, you need to return a PartialViewResult from your call.) The several partial views have one common element, a dropdown, which I'd like to factor out into its own partial view.
But this isn't working. My partial views each have an associated view model, which is their model. For the nested partial view, I'd like to pass the value of a single field, a nullable int, from the parent view's view model, as the model for the nested partial view.
But at run time I get an error saying that my partial view needs a Nullable<int> but received X, where X is the type of the view model associated with the parent partial view.
So my question is twofold:
Is nesting partial views simply not allowed? (In which case, I wish the framework would check for the situation and throw an error that says so explicitly.)
Is there a way to get the effect I want, of a factored-out common interface element, other than with a partial view? I have considered, but not tried, creating an edit template, because I believed that what wouldn't work for partial views wouldn't work for those, but I could be wrong.
ETA: I found my problem: when you pass a null value for the model into HtmlHelper.Partial or RenderPartial, the rendering engine subsitutes the model of the calling partial view in place of that null, assuming that you simply didn't pass a model.
Which is not true in my case: my Nullable<int> is Nullable because, until it's set, it's null! The null is semantically meaningful!
But this is why I was having the problem.
Yes, you can nest partial views. Just make sure you pass in the correct model. HtmlHelpers are useful here, as you can encapsulate the call to RenderPartial with the full view path and ensure the correct model is used.
example
public static void RenderSomePartial(this HtmlHelper helper, int? i)
{
helper.RenderPartial("~/Views/Shared/SomePartial.cshtml", i);
}
Yes, you can. It can get a bit messy if you need to pass models around though. If you're using strongly typed models, try using DisplayTemplates or EditorTemplates instead of partials.
you can use render partial in your parent view and call some child view
also you can pass data with view bag
when you call a partial view from parent the data you pass from controller to parent view can pass to child view
i use the view bag to send my data to child view
I'm trying to use a html.dropDownList helper with a strongly typed view model with ajax. I can't the post the code because of the nature of the project.
Here basically what I'm doing...
loading a mvc view via a strongly type view model
clicking a button which does an ajax post to a controller method
using the TryUpdateModel to parse the view model
processing the request
rendering a parital view for the ajax request
According to the article listed below, the problem is that "ASP.NET MVC assumes that if you’re rendering a View in response to an HTTP POST, and you’re using the Html Helpers, then you are most likely to be 'redisplaying a form' that has failed validation."
http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx
Instead of "redisplaying the same form value", I need the html.dropDownList to be set to the same value in the view model.
Does anyone know of any custom dropDownList helpers or have any ideas of how to achieve this?
Things I've already tried/considered
per the blog, manually removing the modelstate item...didn't work - didn't pick up the value in the view model - just defaulted to the first item in dropdown list
considered just writing a regular select list...but this is sloppy and cumbersome since I'm rending multiple select lists in a loop
writing my own custom dropDownList helper...wanted to avoid reinventing the wheel
Thanks in advance
Its not fully clear to me what your problem exactly is, but I've had a similar problem. I used the Html.DropDownListFor(, SelectListItem[]) helper. At postback it sets the value to the choosen one. Your postback view doesn't require to have all the fields of the original model.
#Html.DropDownListFor(model => model.SelectedValue, MyModels.DropDownSelectables());
Here I want the selected value as the model.SelectedValue variable and within my (seperate) model I made a array of selectlistitems. The rest is automagic.
Hope it helps, D
I have 2 Cotrollers.
First one sets the ViewData property like this
ViewData["Error"] = "something";
I am able display this message on the page.
Second Controller loads the grid.
When i try to set the ViewData property from that Cotroller, It does not show up on the page.
Do you why? Am I doing anything wrong here?
Please let me know.
Thanks!!!!
Using two controllers for a single view is a bit of a no-no.
Look into ViewModels to pass all of the required data to your view. You can then create a PartialView for your grid, and pass the necessary model to the Partial View as well. Consider ViewData / ViewBag a last resort when a ViewModel doesn't work.
Are you trying to use two separate controllers to render the same view? If so you should probably consider, breaking the view logic of your "grid" into a partial view that you in turn render within your primary view.
As you mentioned "ViewData" twice, another item for consideration is the implementation of the ViewModel Pattern. The Viewdata dictionary approach is fast and fairly easy to implement. however, it is not type-safe and errors due to typo's will not be caught at compile time.