Partial View Action does'nt populate model data - asp.net-mvc

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
}

Related

Using ViewModel with a PartialView in ASP.NET MVC

I'm attempting to use a PartialView with a ViewModel but I am getting the error
The model item passed into the dictionary is of type 'Regression', but this dictionary requires a model item of type 'RegressionVM'.
Controller:
public ActionResult _Regression(Regression regression)
{
var model = new ViewModels.RegressionVM(regression);
return PartialView(model);
}
Partial View
#model ViewModels.RegressionVM
<div>
<p>Correlation Coefficient : #Model.Regression.CorrelationCoefficient</p>
</div>
Main View (relevant part)
#Html.Partial("_Regression", SectorAnalysis.evReg)
I've checked that the object passed to the partial controller is not null and is of the correct type.
If in the controller I simply take in a type Regression and pass it to the PartialView that works fine but I get errors whenever I use a view model pattern.
Interestingly if I omit the viewmodel from the partial controller as below the error goes away (obviously I change the partial view to accept #model Regression) :
public ActionResult _Regression(Regression regression)
{
return PartialView(regression);
}
I'm using ASP.NET MVC 4
You need to change this
#Html.Partial("_Regression", SectorAnalysis.evReg)
to this
#Html.Action("_Regression", "ControllerName", SectorAnalysis.evReg)
Rationale:
Html.Partial does not call the controller action, it simply attempts to render the partial view with the model that you sent it. In your case, you are sending a model of type Regression to a partial view that is expecting a model type of ViewModels.RegressionVM. By calling Html.Action(), you are instructing the razor view engine to execute the action in your controller that takes a Regression type object and returns a ViewModels.RegressionVM to the partial view.

Can't Render an MVC View without a Model

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?

PartialView causing error

I have created a partial view which returns a list of items where the ItemsId matches the one passed to it.
//
// GET: /ItemOptions/Item/5
public ActionResult Item(int id = 0)
{
var itemoptions = db.ItemOptions.Where(o => o.ItemsId == id);
return PartialView(itemoptions.ToList());
}
I am trying to display this on the details page of the parent item using the following code:
#Html.Partial("../ItemOptions/Item", Model.ItemsId)
If I visit {URL}/ItemOptions/Item/1 it returns the table I am expecting to see. But if I navigate to the parent item where I am trying to include the partial view I get the following error:
The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[shopServer.Models.ItemOptions]'.
I have read other posts but can't work out where I am going wrong. I also read that I may need to use #Html.Action instead of Partial views, but I am unsure which is appropriate in which situation.
The method Html.Partial is directly rendering the partial view (without executing the controller logic), using the object passed in the second parameter as the model instance. So as the error says, the code line #Html.Partial("../ItemOptions/Item", Model.ItemsId) is trying to render your partial view using an integer (Model.ItemsId) as the model, but your partial view expects IEnumerable[shopServer.Models.ItemOptions].
You have 2 options for resolving your issue, depending if the data for the partial view is already loaded in the model for the parent view or not.
If in the parent view the model already contains the collection of item options, then you can just pass them to the partial view using the Html.Partial method. So assuming you could have a property like ItemOptions in the model of your parent view, then you could render the partial as:
#Html.Partial("../ItemOptions/Item", Model.ItemOptions)
If that is not possible, and the data you need for the partial view is unknown to the parent model, then you need to execute the controller logic (so you can populate the model for the partial view retrieving the extra data from the databaes.) In that case what you need is a child action, achived using the method Html.Action. You will need to pass the controller name, action name and parameters for the action method as in:
#Html.Action("Item", "ItemOptions", new {id = Model.ItemsId})
With the second option, the logic in the controller action method is executed, retrieving the ItemOptions from the database, rendering the partial view and including the resulting html into your main view.
You can find a quick rule of thumb for Html.Partial and Html.Action usage `here.
Hope that helps!

Can I create and set property of MVC3 partial view

Like Asp.Net applications where we create User control(ASCX), and declare some properties for that user control, which we can set from the parent page where we are using the user control, can we do the same thing in Partial View of MVC?
I want to create a partial view for Date picker in MVC, having its validation(enable/disable) property,a flag(display as timepicker or datepicker) and many other such customizable properties, based on which my partial view will behave accordingly.And use this partial view at different places in same page.
You can use RenderAction()
You can call a controller action and pass parameters in here. The Controller action will then return a PartialView (With a model or just ViewBag Values)
public ActionResult DatePicker(bool DoSomething)
{
ViewBag.Something = DoSomething;
return PartialView("DatePicker");
}
and you call this
#Html.RenderAction("DatePicker", "ControllerName", new {DoSomething = true})
Look at Template Editor. This is a sample with a DateTimePicker.
Than you can pass a Model to your partialView for further actions in relation to the model's data.
Create class DatePickerParam like this
public class DatePickerParam{
public boolean isEnabled{get;set;}
//... some other properties
}
call Partial
<%=Html.Partial("~/Views/Shared/MyDatePicker.ascx",new DatePickerParam(){ isEnabled=true})%>
your partial view model class is DatePickerParam

ASP.NET MVC calling partial view from another partial view

I have a page that uses a couple of partial views. My first partial view has some options that when the user selects, and presses a button, it gets data from a database and renders the other partial view on that page.
What is the best way to go about this? I've not really done much in MVC before.
Thanks.
In MVC, views are only concerned with rendering models from your controller. You need to set up a controller action to accept the view options and then render the second partial. Roughly...
[HttpGet]
public ActionResult Foo()
{
return View(); // Foo.aspx is not given a model, so don't show second partial
}
[HttpPost]
public ActionResult Foo(bool option1, string option2)
{
var data = repository.GetData(option1, option2);
var model = new FooModel(data);
return View(model); // Foo.aspx is given a model, so show second partial
}

Resources