[英]ASP.NET Core exclude property of viewmodel when submitting form
我有一個控制器/操作/視圖,其中包含用於生成報告的表單。
public async Task<IActionResult> Report(int? entityId, DateTime fromDate, DateTime toDate)
{
}
在HTTP GET上,我首先檢查entityId
是否為空。 如果是這樣,我假設用戶導航到/report/
來填充表單字段以生成報告,因此我只返回一個包含有關下拉列表等信息的局部viewmodel:
public class ReportViewModel
{
// ...
// this is populated as user has to choose one of these via dropdown
// to generate a report.
public IEnumerable<EntityAccountDTO> EntityAccounts { get; set; }
// ... other properties left unpopulated at this point
}
從下拉列表中選擇EntityAccounts
選項,將使用/report/
action參數中的fromDate
和toDate
值填充兩個日期選擇器。 生成報告時,不需要將其發送回GET操作。
然后,填充視圖中的表單,用戶單擊“獲取報告”。 此時,將執行相同的GET操作,但是entityId.HasValue
為true
, fromDate
和toDate
具有下拉菜單中的值,因此我具有生成報告的所有信息。 然后,我生成一個完整的viewmodel並將其返回到同一視圖。
我看到一些問題:
EntityAccounts
下拉EntityAccounts
被提交回該操作(我可以在查詢字符串中看到它)。 如前所述,這只是供用戶最初選擇一個值,我不需要將其提交回操作。如何防止它被提交?
<label asp-for="@Model.EntityAccounts">Entity accounts</label>
<!-- Input -->
<select asp-for="@Model.EntityAccounts" class="form-control" data-toggle="select">
@foreach () { //... generate <option>s }
</select>
您可以在視圖模型屬性上使用[BindNever]屬性,以防止模型綁定程序進行設置。
[BindNever]
public IEnumerable<EntityAccountDTO> EntityAccounts { get; set; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.