I have looked at most of the available help in SO and google related to this but I'm not sure what I'm doing wrong.
I have Model with some properties, and one of the properties is a list of another complex object. I'm not able to bind this list!
Pleas help!
Here is my Model classes related:
public class PrivacyModel
{
public int RatingId { get; set; }
public List<RatingPoint> RatingPoints { get; set; }
}
public class RatingPoint
{
public int RatingItemId { get; set; }
public string RatingValue { get; set; }
}
Here is my code:
[HttpPost]
public ActionResult Index(PrivacyModel model)
{
.... my business logic....
}
My view looks like this:
@using (Html.BeginForm("Index", "Privacy"))
{
<input type="hidden" name="RatingId" value="@Model.RatingId" />
for (var i = 0; i < Model.RatingPoints.Count; i++)
{
var ratingPoint = Model.RatingPoints[i];
<input type="hidden" name="PrivacyModel.RatingPoints[@i].RatingItemId" value="@ratingPoint.RatingItemId" />
<input type="hidden" name="PrivacyModel.RatingPoints[@i].RatingValue" @("id=RatingPoints" + ratingPoint.RatingItemId) value="@ratingPoint.RatingValue" />
}
<input class="btn" type="submit" value="Submit" />
}
Please don't mind the value and id fields, they are being updated by jQuery somewhere in my page correctly.
This got me a null list of RatingPoints in my action
I have tried also without the prefix PrivacyModel in PrivacyModel.RatingPoints[@i]., but this got me an empty list of RatingPoints in my action
I have also tried using an Index like in the suggested solution here for non-sequential items
@Html.HiddenFor(m => m.RatingPoints[i].RatingItemId)- now inspect the correct name attribute it generates with yours :)