简体   繁体   English

将复选框int数组值分配给变量的问题

[英]Issue with assigning checkbox int array values to variable

I am trying to implement a filter for my search results. 我正在尝试为我的搜索结果实现过滤器。

It is a checkbox which allow user to select one of more options. 这是一个复选框,允许用户选择其他选项之一。 The issue I am having is that the model does not work properly when more than one option is selected. 我遇到的问题是,当选择多个选项时,该模型无法正常工作。

My hunch is that the issue is what is being passed via the viewModel If one value is selected fiddler shows me that beds=1 is passed whereas when multiple values of checkbox are selected then beds[]=1&beds[]=2 is passed. 我的直觉是问题在于通过viewModel传递的内容如果选择了一个值,则提琴手向我展示了beds=1的传递,而当选中复选框的多个值时,则传递了beds[]=1&beds[]=2 Is it because of the brackets [] that the controller is not able to bind it. 是否由于括号[]导致控制器无法绑定它。

More details below 下面有更多详细信息

The view is as follows: 视图如下:

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

This is part of a viewModel which is as follows: 这是viewModel的一部分,如下所示:

public class SearchFormViewModel 
{
   ...
   public int[] beds { get; set; }
}

When the user hits submit - it goes to the search controller via ajax 当用户点击“提交”时-通过ajax进入搜索控制器

  private IEnumerable<Property> ProcessPropertySearch(SearchFormViewModel viewModelInp, int? page = null, int? ps = null) 
{
    var searchQuery = new SearchParameters {
        ...
        BedroomsCounts = viewModelInp.beds ?? new int[0],
        ...             
};

Now, I have tried to debug this. 现在,我尝试调试它。

If I don't choose any filter for bed then viewModelInp.beds = null & BedroomCounts is set to int[0]. 如果我没有为bed选择任何过滤器,则viewModelInp.beds = null&BedroomCounts设置为int [0]。 If I choose one value then viewModelInp.beds = choosen-value & BedroomCounts is set to int[choosen-value] If I choose multiple value then for some reason viewModelInp.beds is flowing in the controller as null. 如果我选择一个值,则viewModelInp.beds =选择值&BedroomCounts设置为int [choosen-value]如果我选择多个值,则由于某些原因,viewModelInp.beds在控制器中流动为null。

I have checked using fiddler as well. 我也检查过提琴手。 For the case, when I choosse multiple values the fiddler webform view shows the following: 对于这种情况,当我选择多个值时,提琴手网络表单视图显示以下内容:

...
beds[]=1
beds[]=2
...

I am not sure why viewModelInp.beds = null when multiple values are choosen. 我不确定选择多个值时为什么viewModelInp.beds = null。

I have used jquery bbq to help bookmark my url. 我使用了jQuery烧烤来帮助为我的网址添加书签。

if I directly write the url for a multiple selection case, ie www.domain.com/search#beds[]=1&beds[]=2 (or,www.domain.com/search#beds=1&beds=2) - then the right checkboxes are shown as 'checked'. 如果我直接为多个选择情况写网址,即www.domain.com/search#beds[]=1&beds[]=2 (or,www.domain.com/search#beds=1&beds=2) -那么右边的复选框显示为“已选中”。 However, the search still ignores it. 但是,搜索仍然会忽略它。

Any help would be grateful! 任何帮助将不胜感激! Thanks 谢谢

--- UPDATE AS ASKED -更新要求

I am using Jquery BBQ. 我正在使用Jquery BBQ。 and the ajax is 而ajax是

$.post('/search/getproperties', $.param(model), function (response) {
 // show properties returned
}

The $.param(model) in the above case is equal to beds[]=1&beds[]=2 在上述情况下,$ .param(model)等于beds[]=1&beds[]=2

I don't think the model binder likes the format it's receiving the data in. 我认为模型绑定器不喜欢接收数据的格式。

You said: The $.param(model) in the above case is equal to beds[]=1&beds[]=2 您说:在上述情况下,$ .param(model)等于bed [] = 1&beds [] = 2

The request body should actually be: beds=1&beds=2 less the [] 请求正文实际上应为: beds=1&beds=2减去[]

As expected, the issue was due to [] being used in the post values due to the way $.param in Jquery BBQ plugin works. 不出所料,该问题是由于Jquery BBQ插件中$ .param的工作方式而在帖子值中使用了[]。

I am not sure if there is a better way to do it but I basically removed the [] from the $.param(model) before posting the values and it worked 我不确定是否有更好的方法,但是在发布值之前,我基本上从$ .param(model)中删除了[]并成功了

so, 所以,

var postValues = $.param(model).replace(/\%5B\%5D/g,'')
$.post('/search/getproperties', postValues, function (response) {
 // show properties returned
}

Please let me know if you guys feel there is a better way. 请让我知道你们是否有更好的方法。 Thank you! 谢谢!

--- UPDATE AS PER ED's Comments ---更新按ED的评论

$.post('/search/getproperties', $.param(model, true), function (response) {
     // show properties returned
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM