簡體   English   中英

為什么 .NET MVC 4 從 GET 查詢字符串而不是模型綁定器中獲取值

[英]Why does .NET MVC 4 take the value from the GET query string instead of the model binder

我有以下 .cshtml 代碼:

@Html.TextBoxFor(x => x.BeginDate, "{0:dd.MM.yyyy}", new { @class = "datefield" })
@Html.TextBoxFor(x => x.EndDate, "{0:dd.MM.yyyy}", new { @class = "datefield" })

通過使用 POST,顯示的值是正確的(也是因為我使用的是模型綁定器)

但是,如果我使用 GET(我使用 PagedList.Mvc),並創建以下查詢: Index?page=3&beginDate=2015-07-01&endDate=2015-07-06 ,以下值將顯示在文本框中: 2015 -07-01 和 2015-07-06,而不是 01.07.2015 和 06.07.2015。

但是,如果我將參數名稱更改為Index?page=3&iBeginDate=2015-07-01&iEndDate=2015-07-06 ,則這些值會在文本框中正確顯示(01.07.2015 和 06.07.2015)。

我的 GET 和 POST 方法的標題如下:

[HttpGet]
public ActionResult Index(int page = 1, int? workCenterId = null, DateTime? beginDate = null, DateTime? endDate = null, int? shiftId = null)
{
    var model = GetModel(page: page, workCenterId: workCenterId, beginDate: beginDate, endDate: endDate, shiftId: shiftId);
    return View(model);
}

[HttpPost]
public ActionResult Index([ModelBinder(typeof(ReportModelBinder))]ReportModel model)
{
     var newModel = GetModel(model);
     return View(newModel);
}

為什么 MVC 更喜歡 GET 參數而不是通過模型發送的參數?

沒有偏好,被調用的動作基於請求方法(GET/POST 等)和動作名稱,默認模型綁定器將使用值提供程序來獲取參數和模型屬性的值,這些值將來自查詢字符串、表單值、cookie、路由數據等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM