MVC ListBox没有将数据传递给操作 [英] MVC ListBox not passing data to Action

查看:79
本文介绍了MVC ListBox没有将数据传递给操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用FireBug时,我看到selectedPrdLctn的选定值已正确设置,但是当我通过JSON将其传递给我的操作时,它为null

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

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

我的动作

   public ActionResult GetCS(String[] cntrParam)

如果我使用dropDownList,则该代码有效.知道为什么我没有通过选择吗?

The code works if I use a dropDownList. Any idea why I'm not passing in the selection?

推荐答案

我怀疑问题出在以下事实:您尚未设置traditional参数,并且jQuery不会以以下格式发送集合:默认模型活页夹是能够理解的.如果您使用的是jQuery 1.4或更高版本,则必须设置此参数.

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. If you are using jQuery 1.4 or later you must set this parameter.

例如:

型号:

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

控制器:

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);
    }
}

查看:

@model MyViewModel

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

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

脚本:

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

请注意我如何使用 $.param 函数并将其作为第二个参数传递给true代表传统参数.尝试用truefalse调用它,您将看到FireBug中的差异,并且您将了解为什么如果不设置此参数,它将不起作用.

Notice how I use the $.param function and pass it true as second argument which represents the traditional parameter. 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.

您还可以为所有AJAX请求全局设置此参数:

You could also set this parameter globally for all AJAX requests:

$.ajaxSetup({
    traditional: true
});

,然后将执行以下操作:

and then the following will work:

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

这篇关于MVC ListBox没有将数据传递给操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆