![](/img/trans.png)
[英]How to resolve issue on assigning values to LINQ variable with Double ToDouble Method
[英]Issue with assigning checkbox int array values to variable
我正在嘗試為我的搜索結果實現過濾器。
這是一個復選框,允許用戶選擇其他選項之一。 我遇到的問題是,當選擇多個選項時,該模型無法正常工作。
我的直覺是問題在於通過viewModel傳遞的內容如果選擇了一個值,則提琴手向我展示了beds=1
的傳遞,而當選中復選框的多個值時,則傳遞了beds[]=1&beds[]=2
。 是否由於括號[]導致控制器無法綁定它。
下面有更多詳細信息
視圖如下:
<div>
<input type="checkbox" name="beds" id="beds_1" value="1" /><label for="beds_1">1</label>
<input type="checkbox" name="beds" id="beds_2" value="2" /><label for="beds_2">2</label>
<input type="checkbox" name="beds" id="beds_3" value="3" /><label for="beds_3">3</label>
<input type="checkbox" name="beds" id="beds_4" value="4" /><label for="beds_4">4</label>
<input type="checkbox" name="beds" id="beds_5" value="5" /><label for="beds_5">5+</label>
</div>
這是viewModel的一部分,如下所示:
public class SearchFormViewModel
{
...
public int[] beds { get; set; }
}
當用戶點擊“提交”時-通過ajax進入搜索控制器
private IEnumerable<Property> ProcessPropertySearch(SearchFormViewModel viewModelInp, int? page = null, int? ps = null)
{
var searchQuery = new SearchParameters {
...
BedroomsCounts = viewModelInp.beds ?? new int[0],
...
};
現在,我嘗試調試它。
如果我沒有為bed選擇任何過濾器,則viewModelInp.beds = null&BedroomCounts設置為int [0]。 如果我選擇一個值,則viewModelInp.beds =選擇值&BedroomCounts設置為int [choosen-value]如果我選擇多個值,則由於某些原因,viewModelInp.beds在控制器中流動為null。
我也檢查過提琴手。 對於這種情況,當我選擇多個值時,提琴手網絡表單視圖顯示以下內容:
...
beds[]=1
beds[]=2
...
我不確定選擇多個值時為什么viewModelInp.beds = null。
我使用了jQuery燒烤來幫助為我的網址添加書簽。
如果我直接為多個選擇情況寫網址,即www.domain.com/search#beds[]=1&beds[]=2
(or,www.domain.com/search#beds=1&beds=2)
-那么右邊的復選框顯示為“已選中”。 但是,搜索仍然會忽略它。
任何幫助將不勝感激! 謝謝
-更新要求
我正在使用Jquery BBQ。 而ajax是
$.post('/search/getproperties', $.param(model), function (response) {
// show properties returned
}
在上述情況下,$ .param(model)等於beds[]=1&beds[]=2
我認為模型綁定器不喜歡接收數據的格式。
您說:在上述情況下,$ .param(model)等於bed [] = 1&beds [] = 2
請求正文實際上應為: beds=1&beds=2
減去[]
不出所料,該問題是由於Jquery BBQ插件中$ .param的工作方式而在帖子值中使用了[]。
我不確定是否有更好的方法,但是在發布值之前,我基本上從$ .param(model)中刪除了[]並成功了
所以,
var postValues = $.param(model).replace(/\%5B\%5D/g,'')
$.post('/search/getproperties', postValues, function (response) {
// show properties returned
}
請讓我知道你們是否有更好的方法。 謝謝!
---更新按ED的評論
$.post('/search/getproperties', $.param(model, true), function (response) {
// show properties returned
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.