I have the following in my _Layout.cshtml:
<title>#ViewData["PageTitle"]</title>
I then have a Child Action that is called, and I want to be able to set change this value in that controller action.
Is this possible?
You could try using the parent context. In your _Layout.cshtml:
<title>#ViewContext.ViewData["PageTitle"]</title>
and in your child action:
[ChildActionOnly]
public ActionResult Foo()
{
ControllerContext.ParentActionViewContext.ViewData["PageTitle"] = "foo";
return View();
}
Related
I have index() action in my controller with
ViewBag.somekey = "somevalue";
MyModel m = new MyModel();
return View(m);
and index.cshtml file with:
#Viewbag.somekey
#Html.Partial("_pv", model)
In index.cshtml, #ViewBag.somekey renders "somevalue", but in "_pv.cshtml" #ViewBag.somekey is blank. "_pv.cshtml" is shared partial view.
Can someone point me what to do here and how to get value of "somekey" in partial view?
well its simple, when ever you are rendering partial view , the ActionResult method which has a partial view must have a defination for ViewBag.somekey = "somevalue"; then only it can show the value for your ViewBag
example
if you want to render this partial view on other ActionResult View suppose
public ActionResult Index2()
{
ViewBag.somekey = "somevalue";
return View();
}
you have to define this ViewBag again
I need to pass data to the action method below from another action method or another view. Then it will be used where the arrow points. But this action method already has a parameter and get value from its own view. And it mustn't have more than one parameter. I don't know how.
Try to use TempData like below examples:
pass data from action to another action
public ActionResult Action_A()
{
TempData["Data"] = "ABC";
return View();
}
public ActionResult Action_B()
{
var data = TempData["Data"];
return View();
}
pass data from action view to another action
Action View
#{
TempData["Data"] = "hello";
}
<p>This is a Demo</p>
Another Action
public ActionResult Action_B()
{
var data = TempData["Data"];
return View();
}
I am Creating a new partial view in MVC 3 as this functionality is required through out my application.Is it possible to have a separate controller for my partial view only
Yes, you can. Create controller (consider to disable non-child action calls if you need to use controller only for partial view):
public class FooController : Controller
{
[ChildActionOnly]
public PartialViewResult Bar()
{
var model = new BarModel();
return PartialView("_Bar", model);
}
}
And use it
#Html.RenderAction("Bar", "Foo")
I have a controller where all of the action methods contain the same code:
[ActionName("pretty-url")]
public ActionResult Something() {
return PartialView();
}
[ActionName("another-pretty-url")]
public ActionResult SomethingElse() {
return PartialView();
}
I name my partial views in the pretty-url.cshtml format, and these get picked up fine and everything works.
As every action in the controller will always do exactly the same thing and return the same thing, I would like to just have my controller look for the correctly-named view and return it as above, without me having to explicitly implement it.
How would I do that?
TIA
I would create a single action and pass the view name as parameter.
public ActionResult Something(string viewName)
{
return PartialView(viewName);
}
I would add a new method to my controller with a string parameter and use it to load the correct partial view.
public ActionResult Show(string PartialName)
{
return PartialView(PartialName);
}
Now instead of using http://your.domain/pretty_url you will have to use http://your.domain/show/pretty_url but this will work with any new partial view you add later on.
I want to return an object on HTTPGet method and different object in HTTPPost method of the same action method in the controller, but i dont know what to write in the view, which model to get.
Here is the controller code , i have been trying
[HttpGet]
public ActionResult Create()
{
var intSrNo = Convert.ToInt64(TempData["sr_no"]);
MEntities obj_entity = new MEntities();
UDP_get_a_Result obj_proc = obj_entity.UDP_get_a(intSrNo).SingleOrDefault();
return View(obj_proc);
}
[HttpPost]
public ActionResult Create(Table_a obj_a)
{
if (ModelState.IsValid)
{
db.Table_a.AddObject(obj_a);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(obj_a);
}
i'm confused which model to write in view.(Table_a or UDP_get_a_Result) and i want both HttpGet to show values when the page is loaded and HttpPost when the submit is clicked.
View
#model ABC.models.Table_a
#{
ViewBag.Title = "Create";
}
A view can only be strongly typed to a single class. You cannot have different controller actions returning the same view and passing different models to this view. You could use view models: define a class which will hold all the information necessary for this view and then have your controller actions fill this view models and pass it to this view.
I think it would work to have the view typed to some base class (object) and then cast the model to whatever you needed it to be based on get/post. I wouldn't want to maintain it tho. :-D