stephen776
stephen776

Reputation: 9234

How to Post Form from View strongly Typed to ViewModel?

I have a view model as such:

public class MyViewModel
{
    public MyObject myObject{ get; set; }
    public List<MyList> myList{ get; set; }
}

I have a view with a form strongly typed to MyViewModel

This view allows you to enter values for the properties of MyObject, as well as create a list of MyList objects. The List part works fine although I thought that would be the more difficult of the two.

Assuming MyObject has a property Description I create a textbox to enter the value as such:

@Html.EditorFor(x => x.myObject.Description);

The text box renders with an id of MyObject_Description...The problem is when I post this to my controller action, MyObject does not get bound at all(althought the list items do as they recieve the appropriate IDs of "MyViewModel_MyList[guid].myListValue")

What am I doing wrong here??

EDIT: more info

The first line of the view is: @model MyApp.ViewModels.MyViewModel

And the Action method:

    [HttpPost]
    public ActionResult Create(MyViewModel myViewModel)
    {

    }

I am passing a new MyViewModel into the partial view to begin...

public ActionResult Create()
    {
        MyViewModel model = new MyViewModel();
        return PartialView(model);
    } 

EDIT 2

Ok When I render my partial view that contains the forms I call :

@{Html.RenderAction("Create", "MyController");}

this is called from within a View of type @model IEnumerable<MyApp.Models.MyObject> (this view displays a list of currently existing MyOjects, and at the bottom the partial is rendered to allow the user to add another MyObject to the DB)

Upvotes: 0

Views: 1512

Answers (4)

AndyM
AndyM

Reputation: 1200

I don't think the out of the box model binding knows how to bind to complex objects. You're probably going to have to write up some sort of custom model binder.

I'm afraid it's not something I've done since MVC1 so I'm a bit hesitant to give you any sample code because the mechanism may well have changed completely since then. A quick google did turn up this article http://www.learnxpress.com/asp-net-mvc-hosting-6-tips-for-asp-net-mvc-model-binding-2.html and this article http://bradwilson.typepad.com/blog/2010/10/service-location-pt9-model-binders.html.

Edit: I've just seen this answer which might help Retrieving data from view, should I use model binder?

Upvotes: 0

sarvesh
sarvesh

Reputation: 2743

If you are not already doing so, try creating a editor template (e.g., Views->Shared->EditorTemplates) called MyObject.cshtml. Move your partial view content to this view and then call

@Html.Editor("myObject"). 

from your parent partial view.

Upvotes: 2

Kon
Kon

Reputation: 27441

I would expect the top line of your view to look something like this:

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<MyViewModel>" %>

This tells the view that the model it is supplied is of type MyViewModel (a la <T> style).

Upvotes: 0

BlackTigerX
BlackTigerX

Reputation: 6146

Change your ViewModel to have the Description directly

public class MyViewModel
{
    public string Description { get; set; }
    public List<MyList> myList{ get; set; }
}

then bind accordingly

@Html.EditorFor(x => x.Description);

Upvotes: 0

Related Questions