How can I send variables to my partial view?
And I don't mean like my model, but values seperate from that
So instead of #Html.Partial("~/Views/Test/_Partial.cshtml", Model)
It would be something like #Html.Partial("~/Views/Test/_Partial.cshtml", Variable = 2)
And then in my partial view I could just use it like
// html
#Variable
// html
You can make the model of your partial the type you want to pass to it:
#model int
In the parent view:
#Html.Partial("~/Views/Test/_Partial.cshtml", 2)
Then access it from the partial view as #Model.
Following are the available options to pass data from a Controller to View in ASP.NET MVC which would be appropriate in your case:
ViewBag
ViewData
TempData
If we want to maintain state between a Controller and corresponding View- ViewData and ViewBag are the available options but both of these options are limited to a single server call (meaning it’s value will be null if a redirect occurs). But if we need to maintain state from one Controller to another (redirect case), then TempData is the other available option which will be cleared once hit.
public ActionResult Index()
{
ViewBag.EmployeeName = “Tushar Gupta”;
return View();
}
View
<b>Employee Name:</b> #ViewBag.EmployeeName<br />
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'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.
I can't use ViewData in master page, and I think it's not a smart way to use ContentPlaceHolder control. For example, I want to transfer a string to master page, how should I do?
Could you give an example?
Use the TempData property to pass data from controller to masterpage. The TempData property value is stored in session state. The value of TempData persists until it is read or until the session times out. If you want pass data one controller view to another controller view then you should use TempData.
public ActionResult NewCustomer()
{
TempData["SomeValue"] = "";
return 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
I'm loading a partial view with an AJAX call:
public ActionResult LoadServerForm()
{
//data stuff
ViewData["ApplicationID"] = appID.ToString();
ViewData["Servers"] = ServersList(appServerRep.Session, null, appServers);
return PartialView("Application_AddServer");
}
This works great, but I'm trying to get away from magic ViewData strings. I tried making the partial view inherit from the same ViewModel as the "hosting" page, but the Model object is null when I try to this in the partial view:
<%= Html.HiddenFor(model=>model.Application_Key, Model.Application_Key) %>
Is there a way to pass the main page ViewModel down into the AJAX-loaded PartialView or should I be looking for a different approach altogether?
When you return PartialView("Application_AddServer");, you have to pass the model:
return PartialView("Application_AddServer", model);
Since this is an AJAX request, it's a separate controller action invocation, and the new PartialView doesn't know about the model of the requesting page. You'll have to reconstruct it, either from whatever your original data source is or from data passed with the AJAX request.