结合查询字符串在ASP.NET MVC对象? [英] Binding query string to object in ASP.NET MVC?

查看:101
本文介绍了结合查询字符串在ASP.NET MVC对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我绑定到IEnumerable的模型网格控件。我会在我的控制器如何过要保存的记录。我使用的网格控件是一个来自Telerik的剑道。请求回是一个字符串,我希望得到我的绑定对象CustomerViewModel怎么过,当我掠过我的对象是回来空。我曾尝试不同类型的信息,似乎为我指定我想传递的属性才有效。请在下面找到code和帮助?

 的[AcceptVerbs(HttpVerbs.Post)
        公众的ActionResult保存([DataSourceRequest] DataSourceRequest要求,CustomerViewModel customerViewModel)
        {
            如果(customerViewModel = NULL&放大器;!&安培; ModelState.IsValid)
            {
                VAR的客户= Repository.GetItems<客户>();
                Repository.SaveChanges<客户,CustomerViewModel,NorthWindDataContext>(客户,customerViewModel);
            }
            返回JSON(ModelState.ToDataSourceResult());
        }


解决方案

如果该对象被嵌​​入在查询字符串,MVC总会看到它作为一个字符串。

您需要做的是这样的:

 公众的ActionResult保存(字符串customerViewString)
    {
        VAR jsonSerializer =新的JavaScriptSerializer();
        VAR customerViewModel = jsonSerializer.Deserialize< CustomerViewModel>(customerViewString);
        如果(customerViewModel = NULL&放大器;!&安培; ModelState.IsValid)
        {
            VAR的客户= Repository.GetItems<客户>();
            Repository.SaveChanges<客户,CustomerViewModel,NorthWindDataContext>(客户,customerViewModel);
        }
        返回JSON(ModelState.ToDataSourceResult());
    }

我一直在挣扎与类似的东西,我不能做一个Ajax Get或Post,您可以在内容类型设置为JSON。似乎没有这样做,在查询字符串(这有一定道理作为它只是一个字符串)的方式。

反序列化它在控制器似乎是唯一的选择。喜欢的人谁可以显示这样做的更合适的方法来听的。

I have a grid control that I have bound to a model of IEnumerable. How ever in my controller I would like to save a record. The grid control I am using is one from Telerik 'Kendo'. The request back is a string and I would like to get my bound object 'CustomerViewModel' how ever when I pass in my object it comes back null. I have tried different types of information and it seems to only work for i specify the property I would like to pass in. Please find code below and assist?

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Save([DataSourceRequest] DataSourceRequest request, CustomerViewModel customerViewModel)
        {
            if (customerViewModel != null && ModelState.IsValid)
            {
                var customers = Repository.GetItems<Customer>();
                Repository.SaveChanges<Customer, CustomerViewModel, NorthWindDataContext>(customers, customerViewModel);
            }
            return Json(ModelState.ToDataSourceResult());
        }

解决方案

If the object is embedded in the query string, MVC will always see it as a string.

You would need to do something like this:

    public ActionResult Save(string customerViewString)
    {
        var jsonSerializer = new JavaScriptSerializer();
        var customerViewModel = jsonSerializer.Deserialize<CustomerViewModel>(customerViewString);
        if (customerViewModel != null && ModelState.IsValid)
        {
            var customers = Repository.GetItems<Customer>();
            Repository.SaveChanges<Customer, CustomerViewModel, NorthWindDataContext>(customers, customerViewModel);
        }
        return Json(ModelState.ToDataSourceResult());
    }

I have been struggling with something similar where I can't do an Ajax Get or Post where you can set the content type to JSON. There seems to be no way of doing that in the query string (which does make sense as its just a string).

De-Serializing it in the controller seems like the only option. Would love to hear from someone who can show a neater way of doing this.

这篇关于结合查询字符串在ASP.NET MVC对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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