I'm having issues rendering a view without a model associated to it. I created a separate MVC project (the out of the box one in VS2013), created a view without a model and the view rendered with no problem. Now for this project, I created a view with its associated controller and ActionResult method in that controller but unless I have a model associated in this view and pass one in it, I get an error "Object reference not set to an instance of an object." I didn't think a view had to have a model associated to it and passed in. Here is what I have so far.
The view is called MainAddress and I'm not associating a model with it. All it has in it is the ViewBag.Title and a paragraph tag with "Test".
Here is the code in the controller:
public ActionResult MainAddress()
{
return View();
}
Here is the view code:
#{
ViewBag.Title = "Main Address";
}
<h1>Test</h1>
Is there a setting that is set somewhere making a model required or am I just doing something wrong?
Related
I'm using Render action to inject some tabs into a calling view. I want to be able to get the Title of the view executing the RenderAction method however in the partial view I can't seem to access the viewbag or viewdata. It was my understanding that a partial view gets a copy of the parents viewbag / viewdata dictionary.
I've tried ViewBag.Title and ViewData["title"] but nothing gets returned. Any ideas?
When you use RenderAction, the model used by that action is independent from the one that is in use when you call RenderAction. The same goes for ViewBag and ViewData. If your action called by RenderAction contains no logic, you could change it to RenderPartial to share the model between parent and child actions.
(Posted answer on behalf of the question author in order to move it from the question post).
I found out that if you create a model you can pass that model into the render actions method:
public class ViewInfo{
public string Title { get; set; }
}
then call the renderaction method:
#{ Html.RenderAction("RenderTabs", "Tab", new {Title = ViewBag.Title});}
I've seen how to use Partial Views from within a View and I understand how a model is passed from the View to a Partial View. What I don't grasp is how to include a Partial View inside of _Layout.cshtml so that I can pass it to the Partial View - or, how the Partial View itself can call a Controller Action to create a Model for use.
Specifically, I want to include a Partial View within _Layout.cshtml to display in the header of every page. The Partial View will display a custom setting in the User Profile. I can't think of any way to obtain the Model without making a Controller Action call - but how is this done in/for a Partial View from within _Layout.cshtml?
Is my only option of accessing a Controller Action to build my Partial View's required Model to use a jQuery call? Or is there another way?
The answer is by calling #Html.Action().
It sounds like you're on the right track.
The key is to try to not pass multiple models around, make models complicated, or use the ViewBag needlessly.
Instead, any time you want information on every page, call an action from your _Layout.
For example, you might have a controller called PartialsController or SharedController.
public class PartialsController : Controller
{
[ChildActionOnly]
public ActionResult UserProfilePartial()
{
UserProfileModel model = new UserProfileModel();
return PartialView("_UserProfile", model);
}
}
The ChildActionOnlyAttribute means that users cannot access the action directly. Only your code can call the action. You can also apply the attribute to the controller so that it affects all actions automatically.
Now call the action from your view (_Layout).
#Html.Action("UserProfilePartial", "Partials")
I am still relatively new to MVC and am finding every new concept to be a struggle, so please forgive me if this is an overly simple concept or the question has been asked many times before (I tried to find other examples).
I have several modals that can be called from my shared layout using jQuery's "dialog." Each modal is simply a DIV with a partial view attached to it like this:
<div id="JoinDialog" title="Join the Contractor Network" style="display: none;">
#Html.Partial("_JoinPartial")
</div>
And is called like this:
$(".ClickToJoin").click(function () {
$(function () {
$("#JoinDialog").dialog({ width: "auto", height: "auto"});
});
});
I have added a "Profile" modal to the layout in which I would like to insert the user's data into the INPUT values. To do that, I presume that I will need to pass in a model, and load the data I want via the controller. Since I currently have this partial view in the "Shared" folder, I assume I will also need to move it to one of my view folders where I can attach it to a controller?
Any nudge in the right direction would be appreciated.
Since I currently have this partial view in the "Shared" folder, I
assume I will also need to move it to one of my view folders where I
can attach it to a controller?
No there is no need for you to move the partial view to the controller folder. You can use the partial view from the shared folder itself (View Engine also looks at Shared folder to find a matching view). Here goes the sample example -
Lets say you have a model like this -
public class MyModel
{
public string Name { get; set; }
}
And then you have an action to return the partial view from the shared folder -
public ActionResult GetPartial()
{
MyModel model = new MyModel();
model.Name = "Rami";
return PartialView("TestPartial", model);
}
Then have the partial view in the Shared folder like this -
#model YouModelNamespace.MyModel
<div>#Model.Name</div>
Then on the actual page, you can have following code -
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#{
Html.RenderAction("GetPartial");
}
That will display the result from the partial view on the page as shown in below screenshot.
When you have to render a View (or a Partial View), asp.net mvc has some default orders to find it. First asp.net mvc will search the respective views' folder of the controller you are executing and if it was not found, asp.net mvc search on the Shared folder. So, if you have a view called _JoinPartial on the Views/Product (for sample) folder and shared folder, it will priorize the View folder. Sometimes you get a exception that views was not found, in there message you can see all places where asp.net mvc find it, for sample:
In your case, the controller could return a Partial View
public ActionResult GetJoinPartial()
{
return PartialView("_JoinPartial");
}
Since you have on the View folder, it will use it, instead it will use the partialView on the Shared folder.
I'm using Umbraco 6, and am using the new MVC architecture. I have a document type view template, which has a view model, which is instantiated and has it's properties populated from the controller. One of these properties is a collection, and in the view template I iterate through the collection and render out a Partial View with a separate view model, using 'Html.Partial("partialName", modelObject)'
The weird problem I'm having is that firstly, in Visual Studio I get a ReSharper warning telling me that it cannot resolve a partial view with that name (I have checked 50 times and I've spelt it correctly). Additionally when I then navigate to the page, I get the trusty ol' "Object reference not set to an instance of an object" YSOD.
I have debugged the code, and the controller action is hit fine, the logic to instantiate the view model for the document type template works fine, and populates the properties correctly, I've also made sure the properties are still set inside the view and the loop for rendering out the collection items correctly instantiates a view model object per collection item and sets the properties correctly. It breaks when it hits the Html.Partial.
Document Type View Code below:
#foreach (KeyValuePair<decimal, IPublishedContent> result in Model.Results)
{
PropertySearchResultViewModel model = ObjectMapper.SearchResultToViewModel(result);
Html.RenderPartial("PropertySearchResultDesktop", model);
}
Partial View Code below:
#using Production.Umbraco.Extensions.Models.ViewModels;
#inherits UmbracoViewPage<PropertySearchResultViewModel>
<article id="property-result-#Model.Node.Id.ToLower()">
<p>#Model.Node.Name</article>
<p>Distance: #Model.Distance Miles</p>
</article>
Here is a screenshot of my VS Solution tree:
The 'NewHomes.cshtml' document type view template is returned from the NewHomesController and the view is calling the 'PropertySearchResultDesktop.cshtml' partial view, which was created from the Umbraco back office, and was placed their automatically.
The Umbraco website says that you can and should place your partial views here
http://our.umbraco.org/Documentation/Reference/Mvc/partial-views
But no matter what I try to do, it just wont render the partial. I've seen one other question about this on SO but the answer was just to place it in MacroPartials instead, which I dont want to do as part of the benefit of using Partial Views in Umbraco 6 is that they inherit from UmbracoViewPage with a strongly typed model declaration, which MacroPartials don't.
Has anyone encountered this before?
Fixed. The issue was with the model I was passing to the document type view from the controller.
In the Umbraco documentation it says, you can create a controller to hijack the Umbraco route and serve up your own view with a custom model, like so:
public ActionResult Index(RenderModel model)
{
SearchResultsViewModel viewModel = new SearchResultsViewModel
return CurrentTemplate(viewModel);
}
and in my view I had:
#inherits UmbracoViewPage<SearchResultsViewModel>
However, it seems as though, in order to do that, you must make sure your custom view model in inherits from RenderModel with a constructor that takes RenderModel as a parameter and then sets some properties on the base object, like so:
public class SearchResultsViewModel :RenderModel
{
public SearchResultsViewModel(RenderModel model) : base(model.Content, model.CurrentCulture)
{
}
}
Previously, my view model had not inherited from anything and had a parameterless constructor.
This article led me to the right answer.
http://www.ben-morris.com/using-umbraco-6-to-create-an-asp-net-mvc-4-web-applicatio
Also, as a side note, I still get the ReSharper warning of "Cannot resolve partial view PropertySearchResultDesktop" but I think that's a ReSharper bug rather than an error.
Even with a full path and file extension in the call, it still complains.
I do find it odd though that while debugging, even with my old controller code, no exception was thrown at the model binding stage or inside the controller, or in the view until it go to the Html.Partial call.
Anyway, I hope this helps anyone having the same issue.
I have a partial view which contains some data from the Model to be displayed. i created an Action of this Partial View in which I return the Model ,I want t display Partial view in each View of my MVC project. but the Problem is that when i return the Model in each View Method Action then it returns the Model and display data but when i am not using to return the Model in each view and want to return the Model only in the Partial view Action then data Model data is not Populating on Partial view .
I want to Return the Model only in the Partial view Action .My code is
public PartialViewResult _FlyMenu()
{
Category cat = new Category();
var category1 =cat.CategoryName;
return PartialView(category1);
}
There isn't anything passed to the partial view action, so there isn't anything that action can pass to the partial view itself.
There should be an argument for the model in the redirect call, and a parameter in the action arguments to receive it.
Assuming your model is of type Category I think you'd need something like this:
public PartialViewResult _FlyResult(Category c)
{
// method body here
}