将列表传递回控制器

时间:2013-07-26 11:41:13

标签: asp.net-mvc-4 model

我有以下型号:

public List<string> Results { get; set; }
    public List<string> ClubcardNumbers { get; set; }
    public bool FindCards {get; set;}
    public bool FindDuplicates { get; set; }
    public bool AllocatedVouchers { get; set; }

我的控制器中有以下操作:

 public ActionResult ImportExcel()
    {


        if (Request.Files["XlFileUpload"].ContentLength > 0)
        {
            string extension = System.IO.Path.GetExtension(Request.Files["XlFileUpload"].FileName);
            string path1 = string.Format("{0}/{1}", Server.MapPath("~/Content/Upload"), Request.Files["XlFileUpload"].FileName);
            if (System.IO.File.Exists(path1))
                System.IO.File.Delete(path1);

            Request.Files["XlFileUpload"].SaveAs(path1);
            Session.Add("XlFileUpload", path1);

        }

        if (Request.Files["DataFileUpload"].ContentLength > 0)
        {
            string extension = System.IO.Path.GetExtension(Request.Files["DataFileUpload"].FileName);
            string path1 = string.Format("{0}/{1}", Server.MapPath("~/Content/Upload"), Request.Files["DataFileUpload"].FileName);
            if (System.IO.File.Exists(path1))
                System.IO.File.Delete(path1);

            Request.Files["DataFileUpload"].SaveAs(path1);
            Session.Add("DataFileUpload", path1);

        }

        MvcUI2.Models.Main model = new Models.Main();
        model.ClubcardNumbers = GetCardNumbers();
        return View("Main", model);
    }

    [HttpPost]
    public ActionResult RunValidation(Models.Main model)
    {
        //determine which validations checks to run
        if (model.FindCards)
        {
            FileReader.UnitTests.pronc_vas ut = new FileReader.UnitTests.pronc_vas();

            ut.FileToCheck = Session["XlFileUpload"].ToString();

            foreach (string cn in model.ClubcardNumbers)
            {
                ut.ClubcardNumberUnderTest = cn.Split('\t')[0];
                ut.ClubcardNumberFound();

                //add results to a list to be read by DisplayResults Action
                if (ut.Result)
                    model.Results.Add("PASSED - " + cn + " - " + "Find card number");
                else
                    model.Results.Add("FAILED - " + cn + " - " + "Find card number");

            }
        }

        return View("Main", model);
    }

以下是我的观点:

<div id="body">
    <div id="ImportExcel">
        <h2>Import</h2>
        @using (Html.BeginForm("ImportExcel", "Main", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <table>
                <tr>
                    <td>Excel file</td>
                    <td>
                        <input type="file" id="XlFileUpload" name="XlFileUpload" /></td>
                </tr>

                <tr>
                    <td>Data file</td>
                    <td>
                        <input type="file" id="DataFileUpload" name="DataFileUpload" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="submit" id="Submit2" name="Submit" value="Submit" /></td>
                </tr>
            </table>
        }
    </div>


    <div id="DisplayCards">
        DisplayCards div
        <ul>
            @foreach (var item in Model.ClubcardNumbers)
            {
                <li>
                    @item
                </li>
            }
        </ul>
    </div>

    <div id="RunValidation">
        @using (Html.BeginForm("RunValidation", "Main"))
        {
            <table>
                <tr>
                    <td>
                        @Html.CheckBoxFor(m => m.FindCards) 
                        Find Cards
                    </td>
                </tr>

                <tr>
                    <td>
                        @Html.CheckBoxFor(m => m.FindDuplicates) 
                        Find Duplicates
                    </td>
                </tr>

                <tr>
                    <td>
                        @Html.CheckBoxFor(m => m.AllocatedVouchers) 
                        Check Voucher Allocation
                    </td>
                </tr>


                <tr>
                    <td></td>
                    <td>
                        <input type="submit" value="SubmitValidation" /></td>
                </tr>
            </table>
        }
    </div>


    <div id="DisplayResults">
        <ul>
            @foreach (var result in Model.Results)
            {
                <li>
                    @result
                </li>
            }
        </ul>
    </div>

用户可以使用html文件输入类型选择Excel文件和数据文件。然后,控制器处理这些文件并将俱乐部卡号列表填充到模型中的列表中。然后在div“DisplayCards”中向用户显示该列表。在div“RunValidation”中,用户可以选择在发回服务器之前选中复选框。此时,我需要在模型中填充控制器的ClubcardNumbers列表,以便RunValidation Action可以处理它。

有人可以建议我怎么做吗?

1 个答案:

答案 0 :(得分:5)

您可以将它们作为隐藏字段包含在表单中,以便在提交此表单时将它们传递给控制器​​:

@using (Html.BeginForm("RunValidation", "Main"))
{
    @for (var i = 0; i < Model.ClubcardNumbers.Count; i++)
    {
        @Html.HiddenFor(x => x.ClubcardNumbers[i])
    }

    <table>
        ...
    </table>
}