Render two Controller/Views on the same page in MVC - c#

Here's my scenario:
I need to create a page such that I have a view containing a DropDownList, then another (partial?) view beneath it that changes depending on what was selected in the DropDownList. I know how to code the DropDownList and make it work with the controllers, but I'm pretty stumped on how to achieve my goal.
The view that will change based on the DropDown has its own controller with CRUD operations (they contain grids). I should add that i'm using a shared view that contains a sort of template for a grid. There's lots of grids, but one view, and a controller for each grid. So I can't just call in the view as a partial view, as it would have no data. I'm essentially needing to call the controller for each grid, which then renders the view containing the grid.
So how can I do this?

you can use partial views to render your views, hence you can use Ajax to update the contents of any element or part of the rendered views dynamically.
you may use jquery to get the selected item value from the drop down list

Related

dropdown list selected index changed razor

I am using razor view engine(cshtml) outside of mvc framework. i.e only view is converted to cshtml format.
I make all database calls using webmatrix and construct cshtml view.
Although, now i want to fire dropdown list selected change event which will set or fill contents of another dropdown list on the same page.
Currently I use dropdown list using
#HTML.DropdownList("ID",List)
on the selectedindex change of dropdown, i wish to check it's value and then decide on whether or not to populate second dropdown.
How can I do it in this scenario? Remember, no mvc framework, hence no access to models or mvc specific methods.
when the dropdown list is rendered in the browser, it simply becomes a HTML tag and you can use vanilla javascript or frameworks like jQuery to check the item selected and load the next dropdown.
Sample:
$( "#myselect option:selected" ).text();

Creating a dynamic WebGrid after user selections (MVC4)

I have several web grids in a single view all working perfectly, except I now need to make one grid dynamic. It will populate once the user makes certain selections in ddl filters. The grid will not have a data source on pageload, I will need to send a json request to the controller but how do i create a placeholder for the grid? I can't use
#grid.GetHTML(...)
because it throws an error wanting a datasource. I was declaring the grid with a model initially:
var gridHistory = new WebGrid(
Commissions.Models.CommissionHistoryModel.getCommissionHistory());
But now that I don't know the data I need until after the user makes selections, I was going to try to do something like this (which throws an error since I'm not supplying a datasource):
var gridHistory = new WebGrid();
How can I supply a datasource when I don't have it at runtime?
you can create partial action (with httpget attribute) returning PartialView which contains your dynamic WebGrid. Each time user make changes to ddl you should render you partial via $.ajax to some placeholder in main view.
I believe the WebGrid has a shortcoming of not doing database level paging. If you have large amounts of data you will have some performance problems.
You might be interested in Dynamic MVC (http://dynamicmvc.com). It will perform dynamic database level sorting, filtering, and paging. It also exposes the the html in partial views so you can customize the html directly in more of an mvc style instead of a webforms control style.

MVC Getting model from partial view

I am new to MVC and i was trying to covert one of my webform project.
I have a request page, depending on drop down i select, controls get populated.
There are 10 request types, so i considered using partial view. I will make an ajax request on select change event, and depending on what is selected, i will return the partial view, but when i submit the main page, how will i retrieve the model for partial view, can i retrieve model separately for main page and partial page.
Yu have no way to pass a model from the client side to the server for rendering a partial view. So, no, that's not possible.
As you want to deliver a different partial view depending on the selected item, you need to pass this information to the browser, so that it can decide which action has to invoke to render the view correspondeing to the selected item. To do so, you can add this information as the value of each element int the drop down list. This value can be something as simple as an id or as complex as a whole url with parameters which invoke the required view. (If you choose the url option, you can render it using the Url.Action url helper extension).
Then, in the code that handles the change event, you can recover the value of the selected item, and use it as a parameter for making the ajax call that will render the required partial view. (For example, if it's the url, you can use jQuery.get() or any of the other jQuery ajax methods with that url).
The ideal situation is that you can render the partial view without depending on the previous rednering of the main view. I.e. the ideal is that you can build the model for the partial by using the action and parameters received in the ajax call.
But, if you need some information that must be generated when rendering the main view, you can use can use TempData to store it when rendering the main view, and to retrieve it when the partial is rendered. (TempData or Session depenging on what yu exactly want to do).

HiddenInput(DisplayValue) needs to be in the CREATE View false but in the LIST View true

I have an enum view model property which I want to hide in the CREATE View but show in the LIST/Index View. The enum is Open,Failed,Succeeded.
For the CREATE View the editor/control should not be visible.
For the LIST View the editor/control should be a selectable combobox
Can this be done somehow with the HiddenInput attribute?
If this can not be done then I want at least to make it visible in both views, but disabled/another control (label instead of combobox).
I am using asp.net mvc 4.0
Actually you should be using different view models. Remember: the first word of view model is view meaning that you define a view model per view. So you will have a CreateViewModel that will be used for the Create view without the enum property in question and a ListViewModel with the enum property that will be used in the Index view.
When doing code reviews I see many developers trying to reuse the same models over different views and ending up with some horrible logic because those models simply are not adapted for those views. They try to hide, they try to write ifs and stuff to perform validation, usually end up in a maintenance nightmare.

asp:Repeater and ActionLink

Am I able to use asp:Repeater and Html.ActionLink together for creating a dynamic menu? Or is there any other methods that I can use it?
Note: I'm getting the menu list from SQL.
I'm going to assume you are using ASP MVC 1 or 2 if you're using HTML.ActionLink. If that is the case, what you'll want to do is pass your list of items to the view through your Model or ViewModel and in the view, create a for each loop to display the items instead of using a repeater control.
Another option is to create a partial view that you pass your list of menu items to and create the for each loop in there then render the partial where ever you need to show your menu.
if you put the menu in the Master Page it will automatically show up every where.
I think you can, there are a lot of information on the web about how to use asp controls in MVC code. also, MVC seems to have its own repeater: http://davidhayden.com/blog/dave/archive/2009/04/07/ASPNETMVCControlsASPNETMVCFuturesRepeaterControlExample.aspx

Categories

Resources