如何将int数组传递给ActionResult?

时间:2015-09-30 14:49:17

标签: c# razor

这是一个简单的问题,但我一直在努力奋斗。我只想向我的ActionResult发送一个int数组参数。每次我尝试它都会返回为空。

查看:

@using (Html.BeginForm("DepartmentReportSelection", "Reports", new {ClientID = Model.inputParameters[0], SupplierID = Model.inputParameters[1], ReviewPeriodID = Model.inputReviewPeriodIDs, StatusCategoryID = Model.inputParameters[2]}, FormMethod.Post)) {
            @Html.DropDownListFor(m => m.CategoryTypeOptions.First().StatusCategoryID, new SelectList(Model.CategoryTypeOptions, "StatusCategoryID", "StatusCategoryDesc"), new { @class = "GRDropDown", @id = "ReportDD" })
            <input type="hidden" name="ClientID" value="@Model.ClientID" />
            if (Model.TypeOfReport == 1) {
                <input type="hidden" name="ReviewPeriodIDs" value="@Model.inputReviewPeriodIDs" />
                <input type="hidden" name="SupplierID" value="@Model.SupplierData" />
            }
            else if (Model.TypeOfReport == 2) {
                <input type="hidden" name="ReviewPeriodIDs" value="@Model.inputReviewPeriodIDs" />
            }
            else{
                <input type="hidden" name="SupplierID" value="@Model.SupplierData" />
            }

            <button type="submit" value="Submit" class="btn btn-default StandardButton">Filter</button>
        }

的ActionResult:

 public ActionResult DepartmentReportSelection(int ClientID, string Supplier, int[] ReviewPeriodID, int? SupplierID = null, int? StatusCategoryID = null) {
            // Cut out unnecessary code 

            return View("DepartmentBreakdown", DepartmentModel);
        }

我试图传递ReviewPeriodID

1 个答案:

答案 0 :(得分:0)

固定。

@using (Html.BeginForm("DepartmentReportSelection", "Reports", new { ClientID = Model.inputParameters[0], SupplierID = Model.inputParameters[1], StatusCategoryID = Model.inputParameters[2] }, FormMethod.Post)) {
            @Html.DropDownListFor(m => m.CategoryTypeOptions.First().StatusCategoryID, new SelectList(Model.CategoryTypeOptions, "StatusCategoryID", "StatusCategoryDesc"), new { @class = "GRDropDown", @id = "ReportDD" })
            <input type="hidden" name="ClientID" value="@Model.ClientID" />
            if (Model.TypeOfReport == 1) {
                for (int i = 0; i < Model.inputReviewPeriodIDs.Length; i++) {
                    <input type="hidden" name="ReviewPeriodID" value="@Model.inputReviewPeriodIDs[i]" />
                }

                <input type="hidden" name="SupplierID" value="@Model.SupplierData" />
            }
            else if (Model.TypeOfReport == 2) {
                for (int i = 0; i < Model.inputReviewPeriodIDs.Length; i++) {
                    <input type="hidden" name="ReviewPeriodID" value="@Model.inputReviewPeriodIDs[i]" />
                }
            }
            else {
                <input type="hidden" name="SupplierID" value="@Model.SupplierData" />
            }

            <button type="submit" value="Submit" class="btn btn-default StandardButton">Filter</button>
        }

您需要在视图中创建一个foreach循环以循环数组并创建3个不同的输入字段。这是我可以将其提交给行动。您也无法通过路线值传递它。