I have a mvc view with a partial view.There is a ActionResult method in the controller which will return a PartialView. So, I need to pass ViewBag data from that ActionResult method to Partial View.
This is my Controller
public class PropertyController : BaseController
{
public ActionResult Index()
{
return View();
}
public ActionResult Step1()
{
ViewBag.Hello = "Hello";
return PartialView();
}
}
In Index.cshtml View
#Html.Partial("Step1")
Step1.cshtml partial view
#ViewBag.Hello
But this is not working. So, what is the correct way to get data from viewbag.
I think I'm following wrong method. Please guide me.
You can use it as mentioned below :
In your View :
#Html.Partial("[ViewName]", (string)ViewBag.Message)
And Your partial View :
#model String
<b>#Model</b>
As Shown Above ViewBag.Message will be passed to the partial view. and in your partial view you can use it as a #Model.
Note : here type of ViewBag.Message is string. You can pass any type.
If you don't have to use ViewBag, you can use TempData. TempData is shared for the whole execution chain.
public class PropertyController : BaseController
{
public ActionResult Index()
{
return View();
}
public ActionResult Step1()
{
TempData["Hello"] = "Hello";
return PartialView();
}
}
In Index.cshtml View
#Html.Partial("Step1")
Step1.cshtml partial view
#TempData["Hello"]
"Child actions follow a different controller/model/view lifecycle than parent actions. As a result they do not share ViewData/ViewBag."
The answer provides an alternate way of passing data.
Does a child action share the same ViewBag with its "parents" action?
Old question but if anyone here to find solution for this question..
You can pass viewbag value to partial with viewdatadictionary.
In your view:
#Html.Partial("_Partial", "", new ViewDataDictionary { { "permalink", ViewBag.Permalink } })
and in partial view use it like this:
ViewData["permalink"]
You can try this in order to pass ViewBag to partial view from action:
Your controller:
public class PropertyController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Step1()
{
ViewBag.Hello = "Hello";
return PartialView("_Partial1", ViewBag.Hello);
}
}
Your view (Index.cshtml):
#Html.Action("Step1")
Your partial view (_Partial1.cshtml):
#ViewBag.Hello
return PartialView("partialviewname", obj);
Related
I am thinking about a scenario rather similar to this question asked some time ago: How to call another controller Action From a controller in Mvc
In the scenario I am thinking of, controller B would however return a partial view that I would like to incorporate into the ViewResult returned by the Home controller, so the code might look like this:
public class Home
{
public ActionResult Index()
{
ActionResult partialViewFromCtrl_B = RedirectToAction(actionName: "SomeAction",
controllerName: "Controller_B");
// The returned view should include the partial view `partialViewFromCtrl_B`
return View();
}
}
public class Controller_B
{
public ActionResult SomeAction()
{
// Do some stuff
return PartialView(viewName: "_SomeAction", model: someModel);
}
}
Is there a way to combine the partial view returned from controller B into the view returned from the Home controller?
If possible, I would like to keep the logic and the views defined for controller B separate from the Home controller rather than merging everything into the Home controller.
Your help is very much appreciated!
Edit:
One possible solution to my scenario might by if the Index.cshtml of the Home controller looks like this
#model SomeModelType
...
#Html.Partial(partialview: "~/Controller_B/_SomeAction", model: Model)
...
and the Index() action of the Home controller would look like this:
public ActionResult Index()
{
// Get the model from Controller_B instead of the partial view
SomeModelType someModelFromCtrl_B = new Controller_B().returnSomeModel();
return View(someModelFromCtrl_B);
}
This however does not seem to be the best approach to take here for me.
So if possible I would like to combine the partial view Controller_B/_SomeAction and the Index view of the Home controller in the action method of the Home controller if that is possible.
Through the code posted I can not understand what you want to return.
based on your little code I think you intend to return Index values in PartialView.
Take a look at the code below:
public class Home
{
public ActionResult Index()
{
if(//cond){
// Do some stuff
return PartialView(viewName: "_SomeAction", model: someModel);
}
else{
ActionResult partialViewFromCtrl_B = RedirectToAction(actionName: "SomeAction",
controllerName: "Controller_B");
// The returned view should include the partial view `partialViewFromCtrl_B`
return View("Index");
}
}
}
I have a controller that looks like this:
[RoutePrefix("items")]
public partial class ItemsController : Controller
{
public ActionResult Index()
{
var model = new MyApp.Models.Items();
return View(model);
}
// POST: /items/delete
[HttpPost]
[Route("delete")]
public ActionResult Delete(DeleteItemModel model)
{
// delete the item
model.Delete();
return RedirectToAction("Index");
}
}
In my Index.cshtml, I have the following:
#model MyApp.Models.Items
<h2>Hello</h2>
<form action="#Url.Action(MVC.Items.Delete())" method="post">
<button type="submit">delete</button>
</form>
The above is clearly a subset of my view. My main concern is, how to pass a DeleteItemModel from my form back to the Delete action. What am I missing? The MVC.Items.Delete path was generated from T4MVC. Yet, I can't figure out how to pass a model into it. Is there a fix here, or do I have to use some other approach entirely?
I'm trying to set up route hijacking on Umbraco 7 with little success.
I have a view called Home.cshtml, the top few lines of which are:
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
Layout = "Master.cshtml";
}
I added a controller:
-EDIT-
updated question to include Index() controller action.
namespace CLIAUmbraco7.Controllers
{
public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
string country = "";
return base.Index(model);
}
public ActionResult Index()
{
return View();
}
}
Sticking a breakpoint on the Layout line catches the site before it loads but HomeController is never called. Any idea what I'm doing wrong?
By default the controller needs to be called
[TheDocumentTypeAliasYouWantToHijack]Controller
If your document type is not "Home" then it will not be intercepted.
You are missing an Index() controller action. Your controller should look like this:
public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
//Do some stuff here, then return the base method
return base.Index(model);
}
}
Take a look at the article on Umbraco website.
I want to use 2 models. The first is on Index.cshtml page, and the second is on _Layout.cshtml page
In the controller which contains the action public ActionResult Index(){...}, I declare some values and return it to View(). Like this:
public ActionResult Index()
{
HomePageViewModel model = new HomePageViewModel();
// do something...
return View(model);
}
And in MyProjectName.Models, I write some classes to check login account and put it on the page _Layout.cshtml. Like this:
On page _Layout.cshtml:
#using MyProjectName.Models
#model MyProjectName.Models.LoginModel
#if (Model.LoginAccount != null)
{
foreach(Account acc in Model.LoginAccount)
{
#Html.ActionLink(#acc.Email, "SomeAction", "SomeController", null, new { id = "loginEmail" })
#Html.ActionLink("Logout", "SomeAction", "SomeController", null, new { id = "logout" })
}
}
The code on page _Layout.cshtml doesn't work. It said that: I have returned a model (HomePageViewModel model), but some values which I want to render is referenced from MyProjectName.Models.LoginModel
Main requirement is: the first model is used to display the content on page Index.cshtml, and the second model is used to check user login (on page _Layout.cshtml).
Can you tell me how to do that? Thank you!
In your layout use Html.Action() or Html.RenderAction() to call a ChildActionOnly method that returns a partial view for LoginModel
[ChildActionOnly]
public ActionResult Login()
{
LoginModel model = // initialize the model you want to display in the Layout
return PartialView(model);
}
and create a partial view that displays the links, then in the Layout
# { Html.RenderAction("Login", "yourControllerName") }
A better approach would be to use partial views and the ViewBag.
in your controller you would do something similar to this:
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Accounts = new AccountsViewModel();
ViewBag.HomePage = new HomePageViewModel();
return View();
}
}
From here you'd pass your model from the ViewBag to a partial view
#{
AccountViewModel Accounts = (AccountViewModel)ViewBag.Accounts;
}
#Html.Partial("_accountPartial", Accounts)
I have a target to create a nested architecture of view/controller. For example:
we have few areas in webpage. All this areas managed by its own controller. In another words I need to create independend pages and then join them in one page.
Please see the image:
So is it possible and where I can read about it? Thanks.
Yes you can achieve this by simply calling
#Html.Action("ActionName", "ControllerName");
Example:
in your case you can do like this
Controller1
public ActionResult View1()
{
return View("View1");
}
Controller2
public ActionResult View2()
{
return View("View2");
}
Controller3
public ActionResult View3()
{
return View("View3");
}
Calling in main page:
#Html.Action("View1", "Controller1");
#Html.Action("View2", "Controller2");
#Html.Action("View2", "Controller2");
call these in different section of main page wherever you want.
You can use #Html.Action() to render child views from different controllers
public class FirstController : Controller
{
public ActionResult Index()
{
return View();
}
}
public class SecondController : Controller
{
[ChildActionOnly]
public ActionResult Method1()
{
return PartialView();
}
}
public class ThirdController : Controller
{
[ChildActionOnly]
public ActionResult Method2(int ID)
{
return PartialView();
}
}
Index View (FirstController)
....
#Html.Action("Method1", "Second")
#Html.Action("Method2", "Third", new { ID = someValue })
You can also use #{ Html.RenderAction(); which is more efficient is you are generating a lot of html.