微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何将视图中的列表传递给控制器​​?

我有一个视图,在里面我正在生成一个列表,但是我没有能够成功地将它传递给控制器​​而没有字段为null:

我的观点代码

@using (Html.BeginForm("AddFOP","Revenue",null,FormMethod.Post,new { enctype = "multipart/form-data" }))
           {
              <table class="grid" style="margin-top: 15px">
                    <thead>
                        <tr>
                            <th>FOP</th>
                            <th>ORGN</th>
                            <th>PROGRAM</th>
                            <th>PERCENTAGE</th>
                            <th></th>

                        </tr>
                    </thead>
                    <tbody>               
                        @for (int i = 0; i < Model.editBillingFOPList.Count;i++ )
                        { 

                         <tr>
                            <td>@Html.TextBox("FOPList[" + @i + "].Fund",Model.editBillingFOPList[i].Fund)</td>
                            <td>@Html.TextBox("FOPList[" + @i + "].ORGN",Model.editBillingFOPList[i].ORGN)</td>
                            <td>@Html.TextBox("FOPList[" + @i + "].Program",Model.editBillingFOPList[i].Program)</td>
                            <td>@Html.TextBox("FOPList[" + @i + "].Percentage",Model.editBillingFOPList[i].Percentage)</td>
                        </tr>

这是控制器:

[HttpPost]
        public ActionResult AddFOP(List<BillingFOPModel> FOPList)
        {


            return View();
        }

我的FOPList显示计数为1,如果我在调试模式下展开它,它会显示模型列表但是具有空值 – 任何想法?

我的型号:

public class BillingFOPModel
    {
        public string BillingArea;
        public string BillingAreaName;
        public string Fund;
        public string ORGN;
        public string Program;
        public string Percentage;
    }

这是我的viewmodel:

public class EditBillingviewmodel
        {   //declare attributes

            public List<BillingFOPModel> editBillingFOPList { get; set; }
            public string newBillingArea { get; set; }
            public string newBillingAreaName { get; set; }
            public string newFund { get; set; }
            public string newORGN { get; set; }
            public string newProgram { get; set; }
            public string newPercentage { get; set; }

            public EditBillingviewmodel()
            {

            }

            //constructor that passes a list and 2 strings
            public EditBillingviewmodel(List<BillingFOPModel> editBillingFOPList,string newBillingArea,string newBillingAreaName)
            {
                this.editBillingFOPList = editBillingFOPList;
                this.newBillingArea = newBillingArea;
                this.newBillingAreaName = newBillingAreaName;
            }

解决方法

你犯的是mvc的新开发者大多数做的同样的错误,你的模型有字段而不是属性,这就是你没有发布数据的原因.

将字段更改为Model类中的属性

public class BillingFOPModel
{
    public string BillingArea {get; set; }
    public string BillingAreaName { get; set;}
    public string Fund { get; set;}
    public string ORGN { get; set;}
    public string Program {get; set;}
    public string Percentage {get; set;}
}

边注:

尽可能使用强类型的助手,如TextBoxFor(),DropDownListFor().

原文地址:https://www.jb51.cc/csharp/99897.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐