查看未通过模型的IEnumerable [英] View not passing Ienumerable of Model
问题描述
修改:删除了部分景观,使事情变得更简单。现在我只需要找出为什么视图不会发布该值
-
ViewModelProspectUsers
ViewModelProspectUsers
public class ViewModelProspectUsers
{
public int Id { get; set; }
public string User { get; set; }
public IEnumerable<ViewModelProspectSelect> Prospects { get; set; }
}
ViewModelProspectSelect
ViewModelProspectSelect
public class ViewModelProspectSelect
{
public int ProspectID { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}
查看
View
@model OG.ModelView.ViewModelProspectUsers
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.Id)
<h5>Please Select Prospects you wish to assign to this User.</h5>
----- 这里是部分曾经是,这些值没有被发布到 [发布]
方法 ------
-----HERE is where the partial used to be, these values aren't being posted to the [Post]
Method------
-----------------------------------------但是他们填充就好了----------------------------------------
-----------------------------------------However they are populating just fine----------------------------------------
@foreach (var item in Model.Prospects)
{
@Html.HiddenFor(x => item.ProspectID)
@Html.DisplayFor(x => item.Name)
@Html.EditorFor(x => item.IsSelected)
}
@*@Html.Partial("_ShowProspectCheckedForUser", Model.Prospects)*@
@*@Html.Partial("_ShowProspectCheckedForuser", new OG.ModelView.ViewModelProspectSelect())*@
<input type="submit" value="Save changes" />
@Html.ActionLink("Cancel", "Index")
}
-
发表
Post
[HttpPost] public ActionResult UsersInProspect(ViewModelProspectUsers viewModel)
如果我是来看看 viewModel.Prospects(M = GT; m.isSelected)
//&LT; - 该值为空不宜
If i were to look at viewModel.Prospects(m=>m.isSelected)
//<- this value is Null shouldn't be
我的视图模型Variableis显示的数据,但不适合了IEnumerable
My viewmodel Variableis showing Data but not for the Ienumerable.
推荐答案
在用列表类型的对象打交道,你必须引用它们使用数组方式有一个方式产生的字段名称,该模型绑定器可以解析他们回来,即
When dealing with list-type objects, you must reference them with array notation to have the field names generated in a way that the modelbinder can parse them back, i.e:
for (var i = 0; i < Model.Count(); i++)
{
@Html.LabelFor(m => Model[i].SomeProperty)
@Html.EditorFor(m => Model[i].SomeProperty)
}
在您的情况,你最好用视图模型包含列表和添加服选
属性的项目,这样就可以跟踪哪些人或未被选中。
In your scenario, you'd be better served by using a view model to contain your list and adding a Selected
property to the items so that you can track which ones were or were not selected.
public class ViewModelProspects
{
public List<ViewModelProspectSelect> Prospects { get; set; }
}
public class ViewModelProspectSelect
{
// Whatever else you have
public bool Selected { get; set; }
}
然后,在你的看法:
Then, in your view:
@model ViewModelProspects
@using (Html.BeginForm())
{
for (var i = 0; i < Model.Prospects.Count(); i++)
{
<label>
@Html.HiddenFor(m => Model.Prospects[i].Id)
@Html.CheckboxFor(m => Model.Prospects[i].Selected, true)
@Model.Prospects[i].Name
</label>
}
}
最后,改变你的操作方法签名:
And finally, change your action method signature:
[HttpPost]
public ActionResult UsersInProspect(ViewModelProspects model)
然后,您可以很容易地获得与动作内选定ID列表:
Then, you can easily get the list of selected ids inside the action with:
var selectedIds = model.Prospects.Where(m => m.Selected).Select(m => m.Id)
这篇关于查看未通过模型的IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!