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

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

Related

How to create a View template for MVC?

I have been given the task of creating a template for Views in MVC. Basically, instead of many Views (CRUD) for each entity, we would only have one that accepts generic model and displays it in Edit/Display mode.
I have so far played around with IView, WebViewPage and ViewPage, but I can't seem to get anything to work. I also searched for something like this, but can't find anything useful really.
Specifically, I don't know which C# class I could overwrite/implement to get my desired effect. Can anybody help me out here?
When using MVC you normally have Razor Views (.cshtml).
These come in 4 flavours:
Full Views
Partial Views
EditorTemplates
DisplayTemplates
From your question, I think, you want EditorTemplates.
Create a new View in Views/Shared/EditorTemplates and name it YourGenericModel.cshtml
Inside this file write the first line:
#model YourGenericModel
You can now specify what should be rendered using the normal Razor syntax.
To Have your model displayed using your new View simply call
#Html.EditorFor(model => model.yourGenericModelInstance)

create several depending entites at once rendered with several partial views - Entity Framework

I got a view, where 3 partial views are rendered:
1. Partial View: Main Entity (Description, Name, etc...)
2. PV: Some Values which need the ID from the Main Entity and an ID from another table
3. PV: another custom Value table (x,y values e.g.) which needs also the Main Entity ID
So my question is how do I bind that correctly, to create these dependend entities at once?
The Main entity is not the problem, rather the other two PVs, because I'm not sure how to be able to add an custom amount of this entries in the view and then send them back altogether to the server. I guess its not possible with Navigation Properties of EF? But when using "Data Transfer Objects" (custom classes) how to handle that?
Do I need to create empty value lists when getting the empty page first?
you may load the n number of the views in just simple steps
JQuery:
<Script>
$("#myPartialView1").load('/URL?Params='+Value);
$("#myPartialView2").load('/URL?Params='+Value);
$("#myPartialView3").load('/URL?Params='+Value);
</Script>
#Using(Html.BeginForm())
{
<div id="myPartialView1"></Div>
<div id="myPartialView2"></Div>
<div id="myPartialView3"></Div>
<input Type="Submit" Value="Submit">
}
All the data present in the views will be posted on the main action method.
Hope this will be help full to you.
Good Luck.
Thanks is always appreciated.
Please Vote.
Actually I am not getting the exact point but please mention the what is the relationships present in the all the partial views may be foreign and primary keys.
Sorry not getting you.Please show the image for further help.

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.

Getting data from partials

I have a main model that contains 3 objects that each line up to a partial view. The model is passed into a form that has 4 partials which each of the partials has part of the model passed into. When my form submits (from the main view, not the partials) I am not getting an data from the partials in the controller. Is there something I am missing? It seems I can send data from the model to the partials, have trouble getting it back on the form submit.
Main View
#model
#Html.BeginForm
{
->Partial1(model.section1)
->Partial2(model.section2)
->Partial3(model.section3)
->Partial4(model.section4)
submit button
}
I can post more info if I didn't explain something very well.
Thanks in advance for the help.
The solution I came up with is EditorTemplates. These allowed me to get the info from the sectioned out bits of the form.
If your partials are collections see http://nuget.org/packages/BeginCollectionItem

MVC3 nested partial views submitt. how does it work?

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...

Categories

Resources