I am trying to post model which contains List<> item looks like below:
public class AddSubscriptionPlanModel
{
public AddSubscriptionPlanModel()
{
AllFeatures = new List<Feature>();
}
public int Id { get; set; }
public int SubscriptionPlanId { get; set; }
public int SubscriptionId { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
[Display(Name = "No Of Users")]
public int? NoOfUsers { get; set; }
[Display(Name = "Duration Type")]
public DurationType DurationType { get; set; }
public int Duration { get; set; }
public decimal Amount { get; set; }
public int SubscriptionPlanTrailId { get; set; }
public int FeatureId { get; set; }
public DateTime CreatedDateUtc { get; set; }
public DateTime LastUpdatedUtc { get; set; }
public int CreatedBy { get; set; }
public int LastUpdatedBy { get; set; }
public List<Feature> AllFeatures { get; set; }
}
I am populating all required fields on get method and feeding List from database
public ActionResult Mapping(int id)
{
var features = FeatureManager.GetAllFeatures();
var model = new Ejyle.DevAccelerate.Web.App.Models.SubscriptionPlan.AddSubscriptionPlanModel();
model.AllFeatures = features;
model.Id = id;
model.SubscriptionId = id;
model.NoOfUsers = 20;
model.SubscriptionPlanId = 1;
model.SubscriptionPlanTrailId = 1;
model.Amount = 1000;
model.CreatedBy = 1;
model.LastUpdatedBy = 1;
model.FeatureId = 1;
model.Duration = 20;
return View(model);
}
And my view code looks like below :
@using (Html.BeginForm("Mapping", "SubscriptionPlans", FormMethod.Post))
{
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.Amount)
@Html.HiddenFor(model => model.Duration)
@Html.HiddenFor(model => model.FeatureId)
@Html.HiddenFor(model => model.CreatedBy)
@Html.HiddenFor(model => model.LastUpdatedBy)
@Html.HiddenFor(model => model.NoOfUsers)
@Html.HiddenFor(model => model.SubscriptionId)
@Html.HiddenFor(model => model.SubscriptionPlanId)
@Html.HiddenFor(model => model.SubscriptionPlanTrailId)
@Html.HiddenFor(model => model.AllFeatures)
<table class="table table-striped">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.IsActive)
</th>
</tr>
</thead>
<tbody>
@for (int i=0; i < Model.AllFeatures.Count; i++)
{
<tr>
@Html.HiddenFor(model => model.AllFeatures[i].Id)
@Html.HiddenFor(model => model.AllFeatures[i].Name)
@Html.HiddenFor(model => model.AllFeatures[i].IsActive)
<td>
@Html.TextBoxFor(model => model.AllFeatures[i].Id)
</td>
<td>
@Html.TextBoxFor(model => model.AllFeatures[i].Name)
</td>
<td>
@Html.EditorFor(model => model.AllFeatures[i].IsActive)
</td>
</tr>
}
</tbody>
</table>
<input type="submit" class="btn btn-success" value="Submit" name="Submit"/>
}
On View, the list items rendered properly and even I am able to edit those fields.
But On post, all properties have the value except AllFeatures list item property which always shows count = 0.
Edit 1: This is how my view looks like while rendering features
Edit 2:
Signature of the method posting to:
[HttpPost]
public ActionResult Mapping(AddSubscriptionPlanModel model)
{
}
