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!