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 开发者_C百科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
精彩评论