Can I create and set property of MVC3 partial view - asp.net-mvc

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

Related

Get title of page that calls render action

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});}

Pass variables to a partial view in MVC

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 />

MVC - How to include a model inside _Layout for Partial View

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")

Partial View Action does'nt populate model data

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
}

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