MVC5 MasterName in View - c#

I have simple Controller Action and knew how to pass model to rendered view but how to get masterName in this view?
public ActionResult Index()
{
var model =1;
return View("Index","some master",model);
}

Related

#Html.ActionLink not wotking at all

I have a weird problem with #Html.ActionLink: it doesn't do anything. It doesn't go to the controller or load the view. I have a project with a menu. From the menu I want to redirect to another view. When I use #Html.ActionLink("Avioane", "Index", "Avioane", null, null) it doesn't go to controller.
public class AvioaneController : Controller
{
// [HttpGet]
public ActionResult Index()
{
//var db = new Curse_AerieneEntities();
//List<Avioane> avs = db.Avioane.Where(z => z.Disponibil).ToList();
//return View(avs);
return View();
}
};
When I use #Ajax.ActionLink("Avioane", "Index", "Avioane", new AjaxOptions{HttpMethod = "GET"}) and set [HTTPGet] on the controller, it goes to the view but doesn't load the view into the page.
What am I doing wrong?

How to load correctly a partial view in mvc?

In my mvc project i have a controller with the following actions:
public ActionResult Index()
{
return View(new List<Product>());
}
The corresponding index view render a partial view with the master grid:
#model System.Collections.Generic.List<TbbModels.Domain.Product>
#Html.Partial("_ProdutoMasterGrid", Model)
This partial view have a submit button and a grid. The client needs to put some data in the form and submit it to that action:
public ActionResult _ProdutoMasterGrid(string param)
{
return PartialView("_ProdutoMasterGrid",
repository.Compare(param).ToList());
}
But than i get the grid without the layout. How can i return a partial view with the layout?
You should explicitly return the correct view:
public ActionResult _ProdutoMasterGrid(string param)
{
return View("Index",
repository.Compare(param).ToList());
}
This ensures that when you do a post to this action, it returns the index view. I'm supposing here that repository.Compare return a List<Product> as well, since the type needs to match.

Change model property in post request asp.net mvc

I have one problem.
This is short example.
This is model.
public class MyModel
{
string Title{get;set;}
}
In view I write
#Html.TextBoxFor(model => model.Title)
This is controller.
public ActionResult EditNews(int id)
{
var model = new MyModel;
MyModel.Title = "SomeTitle"
return View("News/Edit", model);
}
//for post
[HttpPost]
public ActionResult EditNews(MyModel model)
{
//There is problem.When I do postback and
// change Title in this place,Title doesn't change in view textbox
//Only when I reload page it change.
model.Title = "NEWTITLE"
return View("News/Edit", model);
}
It won't change because by default (many think this is a bug) MVC will ignore the changes you make to the model in a HttpPost when you're returning the same View. Instead, it looks in the ModelState for the value that was originally served to the view.
In order to prevent this, you need to clear the ModelState, which you can do at the top of your HttpPost by doing:
ModelState.Clear();

View engine can't find my view

I am getting the following error when trying to return a ViewResult from a post action:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.cshtml
~/Views/Shared/Index.cshtml
~/Views/Home/Home.cshtml
~/Views/Shared/Home.cshtml
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Home.master
~/Views/Shared/Home.master
~/Views/Home/Home.vbhtml
~/Views/Shared/Home.vbhtml
My view is definitely recognised because it works on the GET action.
The code that returns the ViewResult in the POST action is:
return View("Index", "Home", Model);
Here is the view.
Can anybody suggest why this would not be working?
A little more context:
The get action displays the view fine. The post action is actually to a different url but returns the same view. It's the post action that's causing the problem. Both GET and POST actions are on the same controller HomeController.
Here's the (stripped down) controller:
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View(new LoginModelBase());
}
[HttpPost]
public ActionResult Login(UsernameLoginModel Model)
{
...
return View("Index", "Home", Model);
}
}
I've just realised what it is!. I'm using the wrong overload of the View(...) method.
It should be:
View("Index", Model);

Partial view with #HTML.Action(...)

I have a strongly typed partial view that should show the name of an account that a user is logged into:
#model MyNamespace.Models.AccountNameViewModel
#if (Request.IsAuthenticated)
{
#Html.Action("AccountName", "AccountNameController", Model)
Logged in to #Model.AccountName
}
I have a controller:
public class AccountNameController : Controller
{
public ActionResult Index()
{
return View();
}
[ChildActionOnly]
public ActionResult AccountName(AccountNameViewModel model)
{
... Do somthing with the repository to populate the model
return PartialView(model);
}
}
What I want to do is add a shared partial view that displays the name of an account that a user is logged into. What I get is the following error:
The controller for path '/ParentViewPath/' was not found or does not implement IController.
Am I at least heading in the right direction?
You have to remove the controller part in your call
#Html.Action("AccountName", "AccountName", Model)
To render a partial view you can also call
#Html.Partial("AccountName", "AccountName", Model)

Categories