簡體   English   中英

如何將視圖中的列表傳遞給控制器​​?

[英]How do I pass a list from a view to a controller?

我有一個視圖,內部我正在生成一個列表,但我無法成功將其傳遞給控制器​​,而字段為空:

我的觀點代碼:

@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()使用像TextBoxFor()DropDownListFor()這樣的強類型助手。

看看這個: http//www.codeguru.com/csharp/.net/net_asp/mvc/using-display-templates-and-editor-templates-in-asp.net-mvc.htm

使用EditorTemplates意味着您不需要在標記中使用for循環,因此不必擔心在控件上設置正確的名稱和ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM