简体   繁体   English

将对象列表从视图传递到asp.net中的控制器

[英]pass list of objects from view to controller in asp.net

I am adding checkboxes to every object in a List. 我将复选框添加到列表中的每个对象。 When I try to return the values back to the controller the list is empty and I only get the checkbox bool. 当我尝试将值返回给控制器时,该列表为空,并且仅得到复选框bool。

Can someone explain to me how to pass the List correctly from the view to the controller. 有人可以向我解释如何将List从视图正确传递给控制器​​。

I tried it with form but I am not sure if this is the correct way. 我尝试过使用表单,但是我不确定这是否正确。

I searched a lot on Google and also found similar posts on stackoverflow but I couldn't find one that helped me. 我在Google上进行了很多搜索,还发现了关于stackoverflow的类似帖子,但找不到帮助我的帖子。

View 视图

@model List<WCFasp.net.WCF.Person> 
@{
    ViewBag.Title = "ShowView";
}

@using (Html.BeginForm("Check", "Home"))
{
        for (int i = 0; i < Model.Count(); i++)
        {
        <p>@Html.CheckBoxFor(m => m[i].IsChecked) @Html.DisplayFor(m => m[i].Name)</p>
        }
        <input id="submit" type="submit" value="submit" />
}

Controller 调节器

[HttpPost]
        public ActionResult Check(List<WCF.Person> selectedpersonlist)
        {
            //Here I get the empty list

            return View("ShowSelectedView");
        }

Person

[DataContract]
    public class Project
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public bool IsChecked { get; set; }

        public Project(string name, bool isChecked)
        {
            this.Name = name;
            this.IsChecked = isChecked;
        }
    }

Little question at the end. 最后一点问题。 Am I getting down voted because I am not a pro or is there another reason? 我是不是专业人士,还是因为其他原因而被否决?

If you want more properties populated within your list then you can use hidden fields to store the information: 如果要在列表中填充更多属性,则可以使用隐藏字段来存储信息:

for (int i = 0; i < Model.Count(); i++)
{
    @Html.HiddenFor(m => m[i].Name)
    <p>@Html.CheckBoxFor(m => m[i].IsChecked) @Html.DisplayFor(m => m[i].Name)</p>
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.NET ViewModels中填充包含从视图到控制器的字符串列表的对象列表 - How to fill a list of objects that contain a list of string from view to controller in asp.net viewModels ASP.Net MVC:如何使用列表 ViewData object 或从 controller 传递到视图的其他复杂对象 - ASP.Net MVC: How to use a List ViewData object or other complex objects passed from a controller to a view 如何在ASP.NET MVC中使用Razor View从对象列表中获取控制器中的单个对象 - How to get Single Object in Controller from List of Objects using Razor View in ASP.NET MVC 使用 Html.BeginForm() ASP.NET MVC 将对象列表从控制器传递到视图时出错 - Error when passing list of objects from Controller to View using Html.BeginForm() ASP.NET MVC 如何将项目列表从视图传递到控制器 (ASP.NET MVC 4) - How to Pass a List of Items from View to Controller (ASP.NET MVC 4) 将项目从动态加载的视图中添加到列表中,并将其传递给 asp.net 内核中的 Controller - Add item into List from View Loaded Dynamically and pass it to Controller in asp.net core ASP.NET MVC - 如何将列表从控制器传递到视图以填充下拉列表? - ASP.NET MVC - How to pass a list from controller to view to populate dropdown? ASP.NET 内核如何通过列表<int>从视图到 Controller</int> - ASP.NET Core how to pass List<int> from view to Controller ASP.NET MVC 5表将对象从视图传递到控制器 - ASP.NET MVC 5 Table passing objects from View to Controller 从控制器传递数据以在asp.net中查看 - Pass data from controller to view in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM