查看未通过模型的IEnumerable

编程入门 行业动态 更新时间:2024-10-20 07:40:35
本文介绍了查看未通过模型的IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

修改:删除了部分景观,使事情变得更简单。现在我只需要找出为什么视图不会发布该值

  • 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

    本文发布于:2023-10-11 22:55:26,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1483072.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:模型   IEnumerable

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!