简体   繁体   English

MVC ListBox 没有将数据传递给 Action

[英]MVC ListBox not passing data to Action

When I use FireBug I see that the selected value for selectedPrdLctn is being set correctly but when I pass it through JSON to my action, it is a null当我使用 FireBug 时,我看到 selectedPrdLctn 的选定值设置正确,但是当我将它通过 JSON 传递给我的操作时,它是 null

        $.getJSON('@Url.Action("GetCS")', { cntrParam: selectedPrdLctn }, function (getCS) {

My Action我的行动

   public ActionResult GetCS(String[] cntrParam)

The code works if I use a dropDownList.如果我使用 dropDownList,代码就可以工作。 Any idea why I'm not passing in the selection?知道为什么我不通过选择吗?

I suspect that the problem comes from the fact that you haven't set the traditional parameter and jQuery doesn't send the collection in a format that the default model binder is able to understand.我怀疑问题出在您没有设置traditional参数并且 jQuery 没有以默认 model 活页夹能够理解的格式发送集合。 If you are using jQuery 1.4 or later you must set this parameter.如果您使用的是 jQuery 1.4 或更高版本,则必须设置此参数。

For example:例如:

Model: Model:

public class MyViewModel
{
    public string[] SelectedValues { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
}

Controller: Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Items = new[] 
            {
                new SelectListItem { Value = "1", Text = "item 1" },
                new SelectListItem { Value = "2", Text = "item 2" },
                new SelectListItem { Value = "3", Text = "item 3" },
                new SelectListItem { Value = "4", Text = "item 4" },
            }
        };
        return View(model);
    }

    public ActionResult GetCS(string[] values)
    {
        return Json(new 
        { 
            message = string.Format("{0} item(s) selected", values.Length) 
        }, JsonRequestBehavior.AllowGet);
    }
}

View:看法:

@model MyViewModel

@Html.ListBoxFor(
    x => x.SelectedValues, 
    Model.Items, 
    new { 
        id = "mylist",
    }
)

@Html.ActionLink("Send AJAX", "getcs", null, new { id = "mylink" })

Script:脚本:

$(function () {
    $('#mylink').click(function () {
        var selectedValues = $('#mylist').val();
        $.getJSON(this.href, $.param({ values: selectedValues }, true), function (result) {
            alert(result.message);
        });
        return false;
    });
});

Notice how I use the $.param function and pass it true as second argument which represents the traditional parameter.请注意我是如何使用$.param .param function 并将其作为代表传统参数的第二个参数传递给 true 的。 Try with calling it with true and false and you will see the differences in FireBug and you will understand why it doesn't work if you don't set this parameter.尝试使用truefalse调用它,您将看到 FireBug 中的差异,并且您将理解为什么如果不设置此参数它就不起作用。

You could also set this parameter globally for all AJAX requests:您还可以为所有 AJAX 请求全局设置此参数:

$.ajaxSetup({
    traditional: true
});

and then the following will work:然后以下将起作用:

$.getJSON(this.href, { values: selectedValues }, function (result) {
    alert(result.message);
});

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

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