Should I use PartialViewResult or separate method in another contoller? - asp.net

I am using ASP MVC 4 framework. For example, I have 2 controllers: MainPanelController and CartController.
MainPanel controller defines methods and views for showing base main panel functions. Cart controller for example defines standard cart methods: RemoveFromCart, ClearCart, AddItemToCart etc.
Where should I define ShowCartItems method, if I want to display cart items list in main panel index page? I have two choises:
in CartController as PartialViewResult and render it in Index View of
MainPanel controller
completely define it in MainPanelController
I think showing cart items is CartController's task. Or maybe should I define ShowCartItems view in MainPanelController?

Maybe what your are looking for is RenderAction method, which provides you a way to output an action from other controller:
#{
Html.RenderAction("ShowCartItems","CartController");
}

Related

Looking for a way to implement a polymorphic view in ASP.NET Core

I'm working on an ASP.NET MVC Core web app that would have a lot of similar views. Each view is just a simple form that has a list of label&control pairs.
An "Object" editor template has been built in order to generate the content of such views automatically based on view model properties.
Each view would basically have just one line of code:
#Html.EditorFor(m => m)
Each view model is derived from a base class.
Is there a way to get rid of duplicate views and controller actions?
Ideally, I'd like to have just one view where the model is the base model class and one controller action that would receive that base model.
Himanshu's comment steered me to use Partials for the child classes and use the base class as the model for the main views. You may be able to just use reflection/property annotation to populate the views according to the given Model, though I have not tried it.
What I have tried successfully is using a base class that is not abstract and thus will not duplicate properties in the child classes, then using generics like...
List<T> GetChildClasses<T>() where T : BaseClass;
which is called by the controller method and supplies the View Model, and then displaying like this...
#model MyApp.Models.BaseClass
<div class="show_base_class_properties_here">
</div>
#if(Model is ChildClass)
{
#await Html.PartialAsync("_ChildPartial", Model);
}
<div class="whatever_comes_next">
</div>
I suspect there is a better way but this works. Apparently there can only be one #model in a view file.

Can I create an ASP MVC master-details with several models on a single master view?

Lets say I have a master view InvoiceMaster.
I want that view to to contain 2 others partial views at the same time. Those are
ItemView and OrderView. Below is a picture showing what I'm trying to represent :
Data in the black and red form are loaded when the page loads (for now I was only able to get them from #{ ... } at the begining of the page.
So when I click on an Item in the red view, it sends an "id" to the green view... Then I edit the green view...
What I like is to persist data from the black form and from the green form at the same time using a single submit button in the master page
How could I ever do that ?
I thought I could pass models between view but it seems not to be a good idea..
if it is a render partial. you can do that way :
#Html.Partial("ItemView", "Home")
if not and if its a actionresult that way
#Html.Action("OrderView", "Home")
but you must have a code
public ActionResult OrderView(){ return View (); } or puplic PartialView IremView() { return PartiaView(); }
I think you have to use ajax here.
You can wrap you red view into #using (Ajax.BeginForm and set UpdateTargetId to you green view.
Another approach is to use js to send request on item selection and refresh red view on receiving the response.

Render a different view from the same action in Symfony2

I am developing a small system using Symfony2, and I have reached a situation that I need to duplicate the same editAction, but only change the view it renders.
I use this action to edit basic information of all the registered budgets that is listed in a page. I have a special page listing all the inactive budgets, and I want a different editing page to change some statuses and add dates.
How can I use the same editAction to render different views depending on the URLs? The page that lists all the budgets is /budgets and the inactive ones is /budgets/inactive.
Use something like:
/**
* #Route("/budgets/{active}", defaults={"active" = "active"})
*/
public function editAction($active)
{
// ...
}
Then, when you go to /budgets $active will be "active" and when you go to /budgets/inactive $active will be "inactive". You can then use this variable to decide which template to show
More info here:
http://symfony.com/doc/current/book/routing.html#required-and-optional-placeholders
Nice answer by Carlos, thanks, but an edit action requires also validation, and after that the same page with errors or some kind of action on a valid form. So i would say that even a simple form is a bit complex. When you also make a "two views" action it gets 2 * 2 = 4 times more complex. besides of that the $active variable could have any value and not only "active" or as you want "inactive". It could be "joke", "stupid" and a lot more possibilities. My suggestion would be to add a second action and to use private functions for functionality that fits for both forms OR to use a custum class (or service) that handles the forms.

Validation while rendering multiple partial views

I have multiple views/models that are being looped into the main view. I load them via #Html.Partial()...These views/models are basically form elements with certain properties...Unfortunately I soon found out that only the first of each type of view/model is validated. I tried moving the fields around and only the first of each kind would validate.
My partial views look something like this:
#Html.DropDownListFor(model=>model.dropdownVal,Model.SelectItems,new { id=Model.FieldID, Name = Model.FieldID })
I looked at the HTML rendered, and it seems that the validation tags like "data-val" are not applied...
Any ideas would be greatly appreciated!
Add the following at the top of your partials to trick ASP.NET MVC into thinking that the helpers are used inside a form and generate the proper data-val attributes:
#{
this.ViewContext.FormContext = new FormContext();
}
Basically the Html.* helpers are generating data-val clients side validation attributes only if they are placed inside an Html.BeginForm. Except that in your case I guess that this Html.BeginForm is in your parent view and not inside the partial, so the #Html.DropDownListFor doesn't emit any validation attributes. By setting the current FormContext to a new instance in the partial as shown previously, the helper will generate the proper client side validation attributes on the corresponding input field.

How navigate to next page using AJAX in MVC4?

I don't have so much experience using AJAX in a MVC application, in fact is my first facing. Please check the below image and note the rectangles.
The image is just an example that I took from internet.
The biggest rectangle is a partial view in my application and I have to render it when the user press Continue or Continuar button. The application should replace the current view for another without refresh the page.
This is the code which I'm testing, note first that I'm passing the first element of a list, but when the user press the button, render the view with the next element index = 2.
public ActionResult DoTest()
{
if (!Request.IsAjaxRequest())
{ }
List<Worksheet> worksheets = new List<Worksheet>()
{
new Worksheet("Hoja 1", ...),
new Worksheet("Hoja 2", ...)
};
return View(worksheets[0]);
}
Can orient me a little bit to know how to implement this feature? I just know that I need to use Ajax.
Have a look through the tutorials and examples here. There's plenty of other material around on the web with information on this subject.
There are many different ways you can achieve this. One way would be to write a custom paging Helper (HtmlHelper) that accepts new content upon the post event. You can view all about Helpers here : Custom HTML Helpers
Another way could be to use partial page rendering to achieve the partial page update upon post event.
If I was you I would combine a partial view with a jquery function to update the content. You can view some help on that here: Change dive content with Jquery

Resources