当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to pass an int array to an ActionResult?

问题描述:

It's a simple question, yet one I've been struggling with. I'd just like to send a parameter which is an int array to my ActionResult. Every time I have tried it just comes back as null.

View:

@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);

}

I'm trying to pass the in the ReviewPeriodID

网友答案:

Fixed.

@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>
        }

You need to create a foreach loop in the view to loop around the array and create 3 different input fields. This was I could submit it to the Action. You also cannot pass it through the route values.

分享给朋友:
您可能感兴趣的文章:
随机阅读: