gpt4 book ai didi

c# - 无法将数据从 View 传递给我的 Controller

转载 作者:行者123 更新时间:2023-12-04 10:50:25 26 4
gpt4 key购买 nike

我正在尝试将 5 个可选值传递给我的 Controller

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

我的 Controller 是这样的
  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>
}
}

每当下拉菜单之一发生更改时,我都想从主 View 或部分 View 中点击我的 Controller 。我尝试了一个 JS 函数,但它没有提交值
    document.getElementById("numberOfItemsToReturn").addEventListener("change", function () {
document.getElementById("ActivityTwoForm").submit();
})

没有用。如何将下拉菜单中的参数传递给 Controller ​​?

编辑:这是我的部分观点。正在发送部分 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>

最佳答案

添加 [HttpPost]到行动

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

然后修复你的表格
@{using (Html.BeginForm("ActivityManagementTwo", "CallCenter", FormMethod.Post))

将提交按钮添加到表单以测试它
<input type="submit" value="Send" /> 

关于c# - 无法将数据从 View 传递给我的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59506123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com