In a Partial View _MyView.cshtml I need to get the Controller name and the Action used to create the parent calling view.
So if /Equity/Sell was the Controller/Action used to call the view which rendered the partial view _MyView.cshtml , then I need the value /Equity/Sell.
I don't see an object that can do that though.
In the view use:
ViewContext.RouteData.GetRequiredString("controller");
ViewContext.RouteData.GetRequiredString("action");
For posterity in a child action it is like this:
ControllerContext.ParentActionViewContext.RouteData.Values["action"];
ControllerContext.ParentActionViewContext.RouteData.Values["controller"];
Related
A controller action returns a partial view. The parent element and ajax definition for this partial view is generally defined in ajax javascript.
However, is it possible to define the parent element for the partial view in the controller action itself and eliminate the need for Javascript code?
I have layout _PageLayout.
I want to pass some model to layout _PageLayout which can be generate from context that is available in session.
So i want to write Get Action for layout _PageLayout.
You can not pass a model to a layout page in the same way that you can pass a model to a view page.
In the layout page you could call a child action method to retrieve the content you require rather than passing a model to the layout.
For example:
In the _PageLayout.cshtml page you can include a call to the child action like so:
#Html.Action("MyChildActionName", "MyActionName")
And then you can define a child action in the appropriate controller as follows:
[ChildActionOnly]
public ViewResult MyChildActionName()
{
var viewModel = //define view model with the contents from your session value here
return View(viewModel); //This should return a view that will be rendered within the calling View page
}
I want to show a view on some of my forms, which shows a list of alerts, read from a database table. I think I need to use a partial view - but haven't used one.
So far, I created a partial view in my shared views folder called "_Alerts.cshtml".
In that file, at the moment, I simply have:
#{
Layout = null;
}
This is a shared view.
This is just me trying to display something.
And then, on my existing page, on which I want to display the alerts, I have this section of code:
#if (User.Identity.IsAuthenticated)
{
<div class="row">
#Html.Partial("~/Views/Shared/_Alerts.cshtml", null)
</div>
}
This works. However, my understanding is not right. At the moment, I pass no model to it. Is there no controller for the partial view? At the moment, I need to create a controller method - somewhere - that gets me a list of alerts from my data service, and then I want to format that and present it in the partial view. But I am unsure where the controller methods go. If this view is called from 8 different screens, would the 8 controllers for these screens have a call to get my alerts, and format them?
Seems like a lot of duplication.
They need not be duplication.
You can define the action you want inside a controller and call #Html.Action instead of #Html.Partial
Inside you action you can return a partial view.
public class AlertsController : Controller
{
public ActionResult Show()
{
var model = GetModel();//decide where this will come from.
return PartialView("~/Views/Shared/_Alerts.cshtml",model);
}
}
In your layout view or wherever you need to use it. you can simply call it as below.
#Html.Action("Show","Alerts")
If you have all the data you need to pass into the partial, then you can use the #Html.Partial and pass in the model.
If on the other hand, you want the view you are embedding to get the data itself, then you would use Html.RenderAction
I want to pass object data from one view to another on click of button.
I am calling the action to load the second view via ajax.
In the controller action I am trying to pass object data to view. But the view is not rendered.
How can I achieve the scenario of passing data from one view and populating it to the controls of other view?
I want to pass object data from one view to another on click of button
When in any Action or ActionResult you have
return RedirectToAction("controllerName","viewname","parameters")
so the Action Method instead of returning a view, this will redirect to another view Action method and that view will be rendered.
You need to send the data from one view to another in form of query string or post parameters to an action.
Ex:
[HttpPost]
public ActionResult PassData(string dataFromView1)
{
return View("View2", dataFromView1);
}
Note: Here i used a single string data in a parameter. If your View1 has more data/complex data structure, better to use ViewModel class and pass that to your intermediate controller.
I want to pass object data from one view to another on click of button
You need to pass View in HttpPost method. Look at this code.
[HttpPost]
public ActionResult YourActionName(Model model)
{
return View("AnotherActionName", model);
}
Or you can use RedirectToAction.
In a standard MVC application we have _Layout.cshtml and an Index.cshtml view. Now imagine if the user receives an email activation after a registration and now he/she clicks on that link.
The link points to /Account/ActivateAccount and after I process the activation, I wanted to redirect the user to the Login.cshtml partial view displaying a message.
I'm aware I can pass the message via the TempData["Message"] but what I don't know is how to redirect to a partial view and have that display inside the _Layout.cshtml instead that by itself.
At least this is what is happening when I call RedirectToAction("Login", "Home")
Note: all "home" based partial views are displaying within a < div id="divmain"> and my ideal solution would be to be able to decide what view should display inside that div, from the controller.
What's the right way to call a Partial View to be displayed from within another Controller ??
I'm not entirely sure I understand the question but I think what you're asking is if you can call a partial view from the _layout? This can be done with #Html.Action() helper. It will call out to a controller and method you specify and insert the result. The method you call would just be a PartialResult that returns the partial view with whatever data you need.
This is in contrast to #Html.Partial() which will render the partial view in place with whatever data you provide without routing back through a controller.
EDIT:
To summarize the comments to this answer, it seems I misunderstood the requirement. A user receives an email and clicks the link to activate their registration. The controller action that handles that request activates the user and then it needs to redirect to something. In this case, there was already a partial view which would serve the purpose but it couldn't be redirected to directly for some reasons. My suggestion was to just create a new View which contained a #Html.Partial() call to basically wrap the partial in a full view page.
The action method which handled the click could then just return that view directly or, if you consider the click of the link to be a "post" since it changes the application model by validating the user, you would create a new controller action and return RedirectToAction() directly. This would be like following the Post, Redirect, Get method and would prevent some issue if the user tried to refresh the "activated" page. It would also give more control over the URL naming.