传递JSON对象到ASP.NET MVC控制器方法使用的密钥值参数 [英] Passing A JSON Object To An ASP.NET MVC Controller Method Using A Key Value Argument

查看:96
本文介绍了传递JSON对象到ASP.NET MVC控制器方法使用的密钥值参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有可能JSON数据传递到使用一个KeyValuePair类型作为参数的ASP.NET MVC控制器的方法。我有在控制器方法塞子变量断点。当我调试,我有X和Y以下值:

X = 0

Y =空

我怎样才能获得keyValuePair变量从JSON对象正确填充?

 <脚本类型=文/ JavaScript的>
 $(文件)。就绪(函数(){
    $阿贾克斯({
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据类型:JSON,
        输入:POST,
        网址:'@ Url.Action(KeyValuePairTest,家),
        数据:JSON.stringify({
            keyValuePair:{
                重点:1,
                价值:一些文本
            }
        })
    });
 });
< / SCRIPT>
使用System.Collections.Generic;
使用System.Web.Mvc;命名空间MvcApplication1.Controllers
{
    公共类HomeController的:控制器
    {
        公众的ActionResult指数()
        {
            返回查看();
        }        公共无效KeyValuePairTest(KeyValuePair< INT,串> keyValuePair)
        {
            VAR X = keyValuePair.Key;
            变种Y = keyValuePair.Value;
            VAR塞=停止;
        }
    }
}


解决方案

使用自定义的模型粘合剂。当您使用自定义模型绑定,您将有机会获得的形式集合,在这里你将可以读取发布到控制器动作的原始数据。一旦你的数据,你可以把它映射到任何数据结构,包括一键值对。请看看下面的帖子

<一个href=\"http://stackoverflow.com/questions/14841976/persisting-data-back-to-viewmodel-with-a-dynamic-aspx-page-asp-net-mvc-3/14843889#14843889\">Persisting数据传回视图模型与动态.aspx页面中的ASP.NET MVC 3

I'm wondering if it's possible to pass JSON data into an ASP.NET MVC controller method that uses a KeyValuePair type as an argument. I have a breakpoint on the stopper variable in the controller method. When I debug, I have the following values for x and y:

x = 0

y = null

How can I get the keyValuePair variable to populate correctly from the JSON object?

<script type="text/javascript">
 $(document).ready(function () {
    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'POST',
        url: '@Url.Action("KeyValuePairTest", "Home")',
        data: JSON.stringify({
            keyValuePair: {
                Key: 1,
                Value: 'some text'
            }
        })
    });  
 });
</script>


using System.Collections.Generic;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public void KeyValuePairTest(KeyValuePair<int, string> keyValuePair)
        {
            var x = keyValuePair.Key;
            var y = keyValuePair.Value;
            var stopper = "stop";
        }
    }
}

解决方案

Use custom a model binder. When you use a custom model binder, you will have access to the form collection, where you will be able read the raw data posted to the controller action. Once you have the data, you can map it to any data structure, including a key value pair. Please have a look at the following post

Persisting data back to ViewModel with a dynamic .aspx page ASP.NET MVC 3

这篇关于传递JSON对象到ASP.NET MVC控制器方法使用的密钥值参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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