How to load correctly a partial view in mvc? - c#

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.

Related

Update view after form submit

In my MVC controller I have two action methods.
The first one is Index method:
public IActionResult Index()
{
return PopulateViewModel();
}
The "PopulateViewModel" Action Method is used for updating of the view model and then showing these updated values on the Index view.
public IActionResult PopulateViewModel()
{
ViewModel viewModel = new ViewModel()
{
//updating values in the view model
//the values are received when the form in the view is submitted
};
return View("Index", viewModel);
}
The problem that I have is that on my Index view the updated values are not shown immediately after submitting the form in the view. When I submit the form I must then once again refresh the page to see the updated values.
What could be the reason for such behavior and how can I correct that?
You misunderstand the conceptual notion. The index is supposed to represent the initial page state. Other actions within the controller will modify the output by rendering the page with the adjusted model. Or handling server side model binding, but the concept is fundamentally achieving the same result.
Your controller logic should be within the following constraints.
public class SampleController : Controller
{
public IActionResult Index() => new View("...", ...);
public IActionResult SubmitSample(string location)
{
var model = service.GetLabLocations(location);
return View("...", model);
}
}
The index is simulating a GET request, returning the initial page in the required state. The form portion, should POST data, outlined in the SubmitSample portion of the code. This will change the state of the page, but the server will need to render with those changes. So the page will load with the attached model, for you to display.
This would represent Razor more than likely on the server side.
#if(Model != null)
foreach(var sample in Model)
{
// Markup, with the data
}

MVC5 MasterName in View

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

Call another controller and use a different Layout for the called controller action MVC ASP.NET

Am new to MVC ASP.Net. I would like to call an action method from another controller and use a different layout page.
My Views/Shared folder has two layout pages: ~/Views/Shared/_Layout1.cshtml and ~/Views/Shared/_Layout2.cshtml. Below is my code:
//HomeController
public class HomeController : Controller
{
public ActionResult Index(string id)
{
if(String.IsNullOrEmpty(id))
{
//Call someMethod from UserController
//And when called, it should a different Layout page: ~/Views/Shared/_Layout2.cshtml
return View("someMethod");
}
else
{
return AboutPage(id); //uses ~/Views/Shared/_Layout1.cshtml
}
}
public ActionResult AboutPage(string id)
{
return View();
}
}
If you want to use different layout then you can use overloaded version of View method:
return View("view", "_Layout1");
If you want to redirect control flow to different controller/action then:
return RedirectToAction("action", "controller");

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

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