1

I am Francesco and I am newbie. I have a problem with ASP.NET MVC3 regarding the data posted between View and Controller. My goal is to use a form to edit a specific record from my database and then post it back to the controller, which is in charge of checking the record's fields and submit them into the database. The data are passed from the Controller to the View by using a ViewModel and therefore the DefaultDataBinding does not apply in this case. I tried many approaches, I monitored the communication with Fiddler but I couldn't solve my problem. The method described by Phil Haack does not work and neither does the FormCollection.

Here you have the code:

ViewModels: I pass it to the View to create a table. Name and the list of Item names (table columns headers) are just to be displayed. I want to have back the Id, the Month (they are passed correctly as parameters and represents table row headers) and most important the ActualValue of the Items and their Id (I have an association table in my database with Id, Itemid and Month as keys)

public class EditViewModel
{
    [ScaffoldColumn(false)]
    public int Id { get; set; }

    [ScaffoldColumn(false)]
    public int Month { get; set; }

    public string Name { get; set; }

    public List<ItemViewModel> ItemList { get; set; }
}

public class ItemViewModel
{
    [ScaffoldColumn(false)]
    public int ItemId { get; set; }

    public string ItemName { get; set; }

    public float ActualValue { get; set; }
}

View: The View is strongly typed as ViewModels.EditViewModel and sends the fields through a submit button. I have to generate a table by using foreach loops, because I do not know how many items there are in advance

Controller: The controller has to update each kpi passed from the view related to a specific Id and Month.

            [HttpPost]
    public ActionResult EditMonth(int Id, int Month, //FormCollection kpiValues)

I hope I was clear enough and I am looking forward to have some help. Many thanks!

1
  • Sorry Moses, I am new here I have to improve my way of posting questions. My question is "How do I pass a nested list(inside qanother list) with unknown number of elements from a strongly typed View to a Controller?" Commented Mar 25, 2011 at 8:04

1 Answer 1

1

Here's a guess: your controller method (EditMonth) is looking for two parameters named Id and Month. By default, those are the names that the binder will look to find in the request that gets sent back to the server when the user clicks the submit button.

However, I suspect the properties on the rendered page are named something like EditViewModel_Id and EditViewModel_Month. So the binder won't be able to match them up.

You could override the binder's default actions. But I think an easier approach is to redefine the EditMonth method so that it expects an EditViewModel object:

[HttpPost]
public ActionResult EditMonth( EditViewModel model )
{...

The names should then match up (you'll want to check ModelState.IsValid, of course, to ensure that the binder didn't run into problems setting up the EditViewModel object).

BTW, something that I've found helpful is to look at the source in the page rendered in the browser. You can then see the exact names that the binder will be looking for matches on (they're the name=... attributes in the HTML elements).

Sign up to request clarification or add additional context in comments.

1 Comment

Dear Mark, thanks for your reply. The parameters Id and Month are seen correctly by the [HttpPost]EditMonth action method. Those that are not seen at all by the action method are the List<ItemViewModel> ItemList. I also tried to create a post model identical to the EditViewModel except for the Name property (I need it just to display the name in the View). I guess the problem of the binder is when it has to populate a neste list inside the viewmodel...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.