【发布时间】:2017-07-08 17:35:51
【问题描述】:
我正在尝试发布包含 List 项目的模型,如下所示:
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; }
}
我正在填充 get 方法的所有必填字段并从数据库中提供列表
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);
}
我的视图代码如下所示:
@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"/>
}
在查看时,列表项可以正确呈现,甚至我也可以编辑这些字段。
但是在发布时,除了 AllFeatures 列表项属性始终显示 count = 0 之外,所有属性都具有值。
编辑 1:这是我的视图在渲染功能时的样子
编辑 2:
方法的签名发布到:
[HttpPost]
public ActionResult Mapping(AddSubscriptionPlanModel model)
{
}
【问题讨论】:
-
你能给我们一段为循环生成的最终 HTML。我想对于同一属性的隐藏字段和编辑器字段的组合,它可能很难映射一个强类型的对象。
-
为什么需要将
AllFeatures发送到浏览器,然后再将其发回服务器? -
用户将能够编辑视图中的所有功能并回发@CodingYoshi
-
@TravisActon 我试图保留所有属性中的值。我尝试了 hidden for 和 EditFor,但都未能将值从视图保存到控制器。
-
是的,但是在您在此处发布的视图中,您对同一模型属性使用了 editfor 和 hiddenfor。删除 hiddenfor 看看会发生什么,我想当你有重复的属性时尝试自动绑定可能会遇到问题。
标签: c# asp.net asp.net-mvc-4 razor