无法将数据从视图传递到我的控制器

时间:2019-12-27 21:18:35

标签: c# asp.net forms xmlhttprequest

我正在尝试将5个可选值传递给控制器​​

    public class ActivityManagementTwoFiltersModel
    {
    public string timeFilter { get; set; } = "Last 30 days";
    public int numberOfItemsToReturn { get; set; } = 25;
    public DateTime? dateStart { get; set; } = null;
    public DateTime? dateEnd { get; set; } = null;
    public Guid? districtId { get; set; } = null;
    }

我的控制器就是这样

  public async Task<ActionResult> ActivityManagementTwo( ActivityManagementTwoFiltersModel filters)   

我的观点:

  @{using (Html.BeginForm("ActivityManagementTwo", "CallCenter", FormMethod.Get, new { id = "ActivityTwoForm" }))
    {
        <div class="row ">
            <div class="col-md-12" style="float:right;">
                <div class="dateWrapper">@{Html.RenderPartial("_DateFilter"); }</div>
            </div>
        </div>
        <div class="row" style="margin-top: 10px; margin-bottom: 8px;">
            <div class="col-md-3 ActivityManagementTwoHeader" style="font-size:12px; font-weight: bold">
                Displaying Results @Model.Count() of @Model.Count()
            </div>
            <div class="col-md-3">
                Items per page:
                @Html.DropDownList("numberOfItemsToReturn", new List<SelectListItem> {
                new SelectListItem { Value = "25", Text= "25"},
                new SelectListItem { Value = "50", Text= "50"},
                new SelectListItem { Value = "100", Text= "100"},
                })                   
            </div>           
        </div>
    }
}

无论何时更改下拉菜单之一,我都想从主视图或局部视图中击打控制器。我尝试了JS函数,但未提交值

    document.getElementById("numberOfItemsToReturn").addEventListener("change", function () {
    document.getElementById("ActivityTwoForm").submit();
})

没有用。如何在更改时从下拉菜单传递参数给控制器?

编辑:这是我的部分看法。部分View字段正在发送,只有numberOfItemsToReturn而不是

 <table>
        <tr>
            <td style="text-align:right; vertical-align: top; width: 160px">
                Time filter:
            </td>
            <td style="display:inline-block; width:150px" >
                <input id="radio1" name="radioToggle" type="radio"> Recent
                <select id="timeFilter" name="timeFilter" >
                    <option value="Last 30 days">at least 18</option>
                </select>
                <br />
                <input id="radio2" name="radioToggle" class="radioTimeFilter" type="radio"> Date Range
                <div id="dateContent" class="radio-content" style="display:none;">
                    <input id="txtDateOneFilter" type="text" style="width: 70px;" placeholder="Start Date" name="dateStart"/>&nbsp;to&nbsp;
                    <input id="txtDateTwoFilter" type="text" style="width: 70px;" placeholder="End Date" name="dateEnd" />
                </div>
                <br />
            </td>
        </tr>
    </table>

2 个答案:

答案 0 :(得分:1)

[HttpPost]添加到操作

[HttpPost]
public async Task<ActionResult> ActivityManagementTwo( ActivityManagementTwoFiltersModel filters)

然后修复您的表格

@{using (Html.BeginForm("ActivityManagementTwo", "CallCenter", FormMethod.Post))

在表单中添加“提交”按钮以进行测试

<input type="submit" value="Send" /> 

答案 1 :(得分:1)

@Html.DropDownList(m => m.numberOfItemsToReturn, new List<SelectListItem> {
                new SelectListItem { Value = "25", Text= "25"},
                new SelectListItem { Value = "50", Text= "50"},