MVC3 nested partial views submitt. how does it work? - c#

I have started working with mvc3 pattern and am facing a problem.
The problem statement is some what as follows:
I have a partial view lets say MasterPartial which renders some content and is bound with a model named MasterPartialModel which contains all necessary data to display on that partial view. The MasterPartialView has nested partial views uptill n level and the nested child partial view that renders on demand within the MasterPartialView is bound with ChildPartialView.
Further more, the MasterPartial view has the form tag. Now my question is upon submit which model would i be receiving and upto my understanding it would be the model of the view which has the form declared within it. How would I know if a user makes a change in the data rendered by one of the childviews. How would I get models of the child view which were rendered on demand by the user, or simply how do we cater for this kind of a problem in MVC3 pattern?

Fixed the issue by working around the problem and by using jquery to go through all the loaded partial views. Collected all the values required filled an array of the type of a model and sent that array of models to the controller method for processing. :) jquery i awesome specially if coupled with mvc...

Related

How can we use one div one multiple places in View in Mvc

How can we use one div on multiple places in View in MVC.
Suppose I have declared one div and just call that div in multiple frames in Views razor in MVC.
Consider using of partial view for you requirement. So the div you want to show many place will be part of partial view with its own ViewDataDictionary.
Partial Views
A partial view enables you to define a view that will be rendered inside a parent view. Partial views are implemented as ASP.NET user controls (.ascx).
When a partial view is instantiated, it gets its own copy of the ViewDataDictionary object that is available to the parent view.
The partial view therefore has access to the data of the parent view. However, if the partial view updates the data, those updates affect only the partial view's ViewData object. The parent view's data is not changed.
Check this for more information
Or
Another way is using jQuery to get div and inject where it is required.
Create a partial view in Shared folder. Put your common HTML there in that partial view. Use #Html.Partial("_SharedPartialViewName") wherever you want that common html.
You can also learn implementation from this URL
http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC

MVC 4 reuse views and view models best practice

I have 3 screens that share a section(with model data in it(#Html.TextBoxFor)). What is the best way to implement this screens?
What I tried:
1) Partial view for the common section(_ClientData). 3 views for the different screens. 3 view models that have common property(ClientData), that is the view model of the partial.
Problem: If I pass the model to the partial as #{Html.RenderPartial("_ClientData", Model.ClientData);} the data from the partial is not submited to the model.
If I pass the model to the partial as #{Html.RenderPartial("_ClientData", Model);} and reference the properties with a fill name the data is submited, but I can't pass models with different types to the partial view.
2) Use one big View model with all the data required by the 3 screens, one view and show/hide some elements depending on some flag.
Problem: I can't use ValidationAttributes(for example if one field is required in screen 1, but it's not shown in screen 2 and its value is null, the validation will fire). I can use some manual validation in the controller but the whole thing with the all in one view and viewmodel sounds very bad.
Partials are usually not the best choice in case you want to place them inside one form and submit together. In such scenario it is better to take advantage of EditorTemplates which will solve your problem.
Firstly you would have to drag your partials to the folder ~/Shared/EditorTemplates/ and rename them to match the model name.
Then you can call them in your view like this:
Html.EditorFor(model => model.ClientData)
Thanks to this your HTML code (the name attributes to be precise) will be generated in such a way that your default model binder will be able to bind this part of your view as well.

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.

Can I Create a Partial View with Controller

Using ASP.net MVC3 with C#
I want to create a partial view that uses a single database record so I have the View down fine but what controller would I create to actually create the LINQ query or do I need to do it in the View file?
The partial will display a random ad and be on every page on the site.
As M.Babcock says, you really should keep the logic out of the View and into the Controller (or Model if appropriate).
I would pass the resulted data to the View either through a Model, or it little data then just pass it through the ViewBag (or ViewData).

Two forms, one page. View Model, Model or something else?

I have a component that deals with uploading images, it works fine when in its own form it is a view bound to a view model, mapped to the main model in the controller, and similarly I have a standard view that is bound to a simple view model, and then mapped to the main model and saved.
So, both of these work fine as separate pages, however, I am keen to present them to the user in the same page - and I am completely stuck.
I have two different View Models required for this one page and just not sure how to go forward, and how to combine them.
I have tried making a new Viewmodel which basically contains the two other View Models, but when either of the forms are submitted, ModelState.IsValid always returns false as some of the required data in other fields is not present.
By getting rid of ModelState.IsValid, the application works fine, but as a MVC newbie, I feel a bit uneasy with this and just wondering if anyone can help me?
(And if this does require a new ViewModel with a ViewModel for each form, Bonus points if you can tell me a good naming convention as the few I have tried just look really messy!)
You could try using Partial Views.
<div>
#Html.Partial("FormA")
</div>
<div>
#Html.Partial("FormB")
</div>
Then in FormA.cshtml, you'll have:
#model Namespace.FormAViewModel
<form> </form>
And then similar for FormB

Categories

Resources