DropDownListFor表单提交未触发

时间:2015-04-17 09:46:28

标签: asp.net-mvc-4 html.dropdownlistfor

我有一个过滤器下拉列表:

@Html.DropDownListFor(x => x.selectedDateFilter, 
                      new SelectList(Model.bydatefilter, "id", "dt", Model.selectedDateFilter), 
                      "By Date", 
                      new { onchange = "this.form.submit();", @class = "form-control" })

当我改变选择时,我什么都没有。

这是页面来源:

<select class="form-control" 
        data-val="true" 
        data-val-number="The field selectedDateFilter must be a number."
        data-val-required="The selectedDateFilter field is required."
        id="selectedDateFilter" name="selectedDateFilter" 
        onchange="this.form.submit();"><option value="">By Date</option>
                                       <option value="1">April-2015</option>
                                       <option value="2">May-2015</option>
                                       <option value="3">June-2015</option>
                                       <option value="4">July-2015</option>
                                       <option value="5">August-2015</option>
                                       <option value="6">September-2015</option>
                                       <option value="7">October-2015</option>
                                       <option value="8">November-2015</option>
                                       <option value="9">December-2015</option>
                                       <option value="10">January-2016</option>
</select>

我注意到这个主题的所有答案都使用DropDownList而不是&#39; for&#39;。是否有一个原因?为什么控件没有回发?

编辑:我传递的模型是一个设置为零的整数。想法是它会在帖子之后按索引设置选定项目。

public int selectedDateFilter { get; set; }
selectedVenueFilter = 0;

这是错误在源中创建以下内容的地方:

data-val="true" 
data-val-number="The field selectedDateFilter must be a number."

@Html.DropDownListFor(x => x.selectedDateFilter, 
                      new SelectList(Model.bydatefilter, "id", "dt", Model.selectedDateFilter), 
                      "By Date", 
                      new { onchange = "this.form.submit();", @class = "form-control" })

我已经检查了x.selectedDateFilter = 0的值。我也尝试将其设置为1并仍然是错误。

1 个答案:

答案 0 :(得分:0)

你需要做的如下:

@Html.DropDownListFor(x => x.selectedDateFilter, 
                      new SelectList(Model.bydatefilter, "id", "dt", Model.selectedDateFilter), 
                      "By Date", 
                      new { onchange = @"this.form.submit();", @class = "form-control" })

您需要在@

之前附加this.form.submit();