Get title of page that calls render action - asp.net-mvc

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

Related

MVC RenderAction return only renders partial part

I have this contact form which I want to use on two pages (two views) in MVC 5.x (razor viewengine) So I have put the form in an partialview called _Contact and I have read that RenderAction is the best approach if you do not have the required data for the partialview in your model and if it is more standalone (seperate from the rest of the view)
So I call it like this:
#{ Html.RenderAction("SendMail", "Uk");}
My Uk controller has these two methods:
[HttpGet]
public PartialViewResult SendMail()
{
return PartialView("_Contact");
}
[HttpPost]
public PartialViewResult SendMail(FormCollection fc)
{
// send mail using values out of the form (sorry did not feel like building a complete model for it
ViewBag.Succeed = true;
// if smtpclient could not reach server etc. it returns false
return PartialView("_Contact");
}
it all works, but the PartialView is only rendered, not on the placeholder where i call the RenderAction. It works all great, but after the post it just displays the partial view and not the "parent view" and the shared layout view etc. I hope that I made myself clear. Please let me know if I need to add more info.
This is the BeginForm from my shared view:
using (Html.BeginForm("SendMail", "Uk", FormMethod.Post))
It will not work as expected for your current code, because when you post the form, it returns Partial View not complete View. If you want to get only partial view then you have to submit your form via Ajax.
In ajax's success handler you will get HTML of your partial view and that you can put in a DIV tag of partial view container.
This link will give you a better idea about Posting Partial View via Ajax.
ASP.NET MVC Partial view ajax post?

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

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 partial view that updates without controller

I have a partial view that shows a list of Categories. I'd like to put that partial view on any page, but I'd like to have it to call to the service and get a list of categories by itself without me having to do that in every controller action. Something like webforms in which you can put a code-behind on it.
For eg.
Actions
public ActionResult Index()
{
JobListViewModel model = new JobListViewModel();
model.Categories= jobService.GetCategories();
return View(model);
}
public ActionResult Details(int id)
{
Job job = jobService.GetJob(id);
return View(job);
}
I created a partial that will take the model.Categories model and display a list. As you can see, the Index page will work fine, but I do not want to call it again in the Details page. Is there a way to make my partialview call to the GetCategories() service by itself?
Use Html.RenderAction - that gives the partial view its own controller action.
You should also mark you partial action with the attribute [ChildActionOnly].
DVark,
As noted in the accepted answer, for your scenario, RenderAction is the most appropriate.
I thought I'd link a little article that distils my thinking on the topic (i.e. when to use RenderPartial vs RenderAction):
http://cbertolasio.wordpress.com/2010/09/21/mvc-html-renderaction-vs-html-renderpartial/
hope it helps
[edit] - as an aside. a year or so ago, i got myself into a few scrapes by not appreciating the power of RenderAction, in favour of RenderPartial. as a result, i had littered the shared view space with lots of partialviews in order to access them from a variety of sources. the moral of the story: know your 'territory' before planting your flag.

Problem with strongly typed partial view

I've a problem with Partial View.
I am developing a blog in asp.net mvc and I would make in my masterpage a list of categories, last post, last comments.
I think that the best solution is to use strongly typed partial view, and in each partial view pass the necessary model.
MY problem is that the model in View.. in any view (connected to the masterpage's contentplaceholder) enter in conflict with the models in partial views and I get an error like this:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Blog.Models.Articoli]' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Blog.Models.Categorie]'.
I found on web a dirty solution that consist of to pass togheter the model of any view, some viewdata, one for every model to pass in partial view. But this solution don't respect DRY Principle..because you must repeat this code for each action!
So, my question is: Can I create a model that contain also partial view's model?
If, yes, in that way?
It Exist another solution more simple?
Thanks for help
How about the View Model Pattern?
I've created wrapper classes that are passed to my views rather than whatever object I would normally use
public class MyCreateUserView
{
public User CreatingUser { get; set; }
public MyPartialViewObject Blah { get; set; }
}
In your view write:
public ActionResult CreateUser()
{
MyCreateUserView createUser = new MyCreateUserView()
{
CreatingUser = GetUserFromSomewhere(),
Blah = GetPartialViewObject();
}
return View(createUser);
}
Then your page header looks like so:
<%# Page Language="C#" Inherits="ViewPage<MyCreateUserView>" %>
and when you render your partial write:
<% Html.RenderPartial("../MyPartialViewObject ", Model.Blah); %>
Instead of solving that with the pattern you describe (which is generally a great pattern), I solve that with calls to RenderAction and have it return a partial view. That way the code is in one place as each call to each view does not have to worry about marshalling all the data you need. If you want to see a short discussion on how to use it, I would check Haack's blog here: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx. You can also check out the discussion on this other post here on SO: ASP.NET MVC Master Page Data

Resources