asp.net mvc将IEnumerable <model>从视图传递给控制器

时间:2018-01-10 15:38:48

标签: asp.net-mvc model controls

我有一个IEnumerable模型,在填充后会在表格中的VIEW中显示确认,然后用户点击导入按钮完成导入过程,所以我需要调用import action方法来做到这一点我还需要通过作为参数的导入动作方法的模型....(注意我的模型不是数据库模型......只是一个简单的类).....这是我的模型

public class Dairy_XL
    {
        public int No { get; set; }
        public string RegNo { get; set; }
        public string NAAB_CODE { get; set; }
        public string NAME { get; set; }
        public string ICC { get; set; }
        public string LNM { get; set; }
        public string LFM { get; set; }
        public string MILK { get; set; }
        public string FAT { get; set; }
        public string PRO { get; set; }
        public string SCE { get; set; }
        public string PL { get; set; }
        public string DPR { get; set; }
        public string PTAT { get; set; }
        public string UDC { get; set; }
        public string FLC { get; set; }
        public string TPI { get; set; }
        public string SIRE { get; set; }
        public string MGS { get; set; }
        public string FEE { get; set; }
        public string CAP { get; set; }
        public string X_FLAG { get; set; }
        public string STAT { get; set; }
        public int ACTION_FLAG { get; set; } 

        //----------------------------------------------------------------

        public bool color_reg { get; set; }
        public bool color_naab { get; set; }
        public bool color_name { get; set; }
        public bool color_icc { get; set; }
        public bool color_lnm { get; set; }
        public bool color_lfm { get; set; }
        public bool color_milk { get; set; }
        public bool color_fat { get; set; }
        public bool color_pro { get; set; }
        public bool color_sce { get; set; }
        public bool color_pl { get; set; }
        public bool color_dpr { get; set; }
        public bool color_ptat { get; set; }
        public bool color_udc { get; set; }
        public bool color_flc { get; set; }
        public bool color_tpi { get; set; }
        public bool color_fee { get; set; }
        public bool color_cap { get; set; }

    }
}

这是我的确认VIEW,我尝试使用动作链接调用动作方法并传递模型(最后一行)

@model IEnumerable<Javad_New.Models.Dairy_XL>
.......    
<div class="container-fluid" style="overflow-x: auto; margin-top: 50px">

        <table class="table table-bordered table-striped text-center">
            <thead>
                <tr style="background-color: darkcyan; color: white">
                    <th>No.</th>
                    <th>RegNO</th>
                    <th>NAAB CODE</th>
                    <th>NAME</th>
                    <th>ICC</th>
                    <th>LNM</th>
                    <th>LFM</th>
                    <th>MILK</th>
                    <th>FAT</th>
                    <th>PRO</th>
                    <th>SCE</th>
                    <th>PL</th>
                    <th>DPR</th>
                    <th>PTAT</th>
                    <th>UDC</th>
                    <th>FLC</th>
                    <th>TPI</th>
                    <th>SIRE</th>
                    <th>MGS</th>
                    <th>FEE</th>
                    <th>موجودی</th>
                    <th>X-FLAG</th>
                    <th>STAT</th>

                </tr>


            </thead>

            <tbody id="mytbody3">

             @foreach (var d in Model)
             {
                <tr>

                    <td>@d.No</td>

                    @if (d.color_reg)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.RegNo)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.RegNo)</td>
                    }

                    @if (d.color_naab)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.NAAB_CODE)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.NAAB_CODE)</td>
                    }

                    @if (d.color_name)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.NAME)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.NAME)</td>
                    }

                    @if (d.color_icc)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.ICC)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.ICC)</td>
                    }

                    @if (d.color_lnm)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.LNM)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.LNM)</td>
                    }

                    @if (d.color_lfm)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.LFM)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.LFM)</td>
                    }

                    @if (d.color_milk)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.MILK)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.MILK)</td>
                    }

                    @if (d.color_fat)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.FAT)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.FAT)</td>
                    }

                    @if (d.color_pro)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.PRO)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.PRO)</td>
                    }

                    @if (d.color_sce)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.SCE)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.SCE)</td>
                    }

                    @if (d.color_pl)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.PL)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.PL)</td>
                    }

                    @if (d.color_dpr)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.DPR)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.DPR)</td>
                    }

                    @if (d.color_ptat)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.PTAT)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.PTAT)</td>
                    }
                    @if (d.color_udc)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.UDC)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.UDC)</td>
                    }
                    @if (d.color_flc)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.FLC)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.FLC)</td>
                    }
                    @if (d.color_tpi)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.TPI)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.TPI)</td>
                    }

                    <td>@Html.DisplayFor(modelitem => d.SIRE)</td>
                    <td>@Html.DisplayFor(modelitem => d.MGS)</td>

                    @if (d.color_fee)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.FEE)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.FEE)</td>
                    }
                    @if (d.color_cap)
                    {
                        <td style="background-color:lightcoral">@Html.DisplayFor(modelitem => d.CAP)</td>
                    }
                    else
                    {
                        <td>@Html.DisplayFor(modelitem => d.CAP)</td>
                    }

                    <td>@Html.DisplayFor(modelitem => d.X_FLAG)</td>

                    @if (d.ACTION_FLAG == 1)
                    {
                        <td style="white-space:nowrap;background-color:lightcoral">@Html.Raw(d.STAT)</td>
                    }
                    else if (d.ACTION_FLAG == 2)
                    {

                        <td style="white-space:nowrap;background-color:lightskyblue">@Html.Raw(d.STAT)</td>
                    }
                    else if (d.ACTION_FLAG == 3)
                    {
                        <td style="white-space:nowrap;background-color:lightgreen">@Html.Raw(d.STAT)</td>
                    }

                </tr>
              }                   


            </tbody>


        </table>


    </div>


    <div class="container">

        <div class="col-md-4"> </div>
        <div class="col-md-4">
            @Html.ActionLink("IMPORT", "Import_Dairy_XL", new { xl_model = Model })
        </div>
        <div class="col-md-4"> </div>
    </div>

但在我计算导入操作中的列表后,它显示0。

 public ActionResult Import_Dairy_XL(List<Dairy_XL> xl_model)
        {

            //Dairy_XL dairy_xl_model = new Dairy_XL();

            //ViewBag.test = xl_model.Where(x => x.RegNo == "123").Select(y => y.RegNo).SingleOrDefault();
            ViewBag.test = xl_model.Count;



            return View();
        }

1 个答案:

答案 0 :(得分:0)

使用ActionLink方法将整个集合作为操作方法参数传递使用HttpGet方法是没有意义的,因为集合项将在URL中转换为查询字符串并可能超出其限制。只需使用该集合的ID属性(通常是int属性)作为参数,如下所示:

@Html.ActionLink("IMPORT", "Import_Dairy_XL", "ControllerName", new { no = d.No })

上面给出的ActionLink应该放在包含集合项的foreach循环视图页面中。

然后,将指定的ID属性作为操作方法参数传递,在此示例中给出:

// somewhere in controller declaration
List<Dairy_XL> list = new List<Dairy_XL>() { new Dairy_XL() { ... }, ... };

// action method part
[HttpGet]
public ActionResult Import_Dairy_XL(int no)
{
    // use LINQ to query that list with passed value from property
    Dairy_XL model = list.Where(x => x.No == no).SingleOrDefault();

    // other logic

    // put something to pass into view
    return View(model);
}

之后,您可以创建另一个显示查询结果的视图:

@model Javad_New.Models.Dairy_XL

@* ID property as example *@
<p>@Model.No</p>

<!-- other properties -->

您可以看到类似的问题here