MVC - 传递整个模型的参数Url.Action在JavaScript [英] MVC - Pass entire model as parameter to Url.Action in javascript

查看:392
本文介绍了MVC - 传递整个模型的参数Url.Action在JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过整个模型的参数Url.Action或类似的?

其实我参数传递给控制器​​和I装载模式,但我会通过整个模型

  window.location.replace('@ Url.Action(搜索,搜索,新的{idSong = Model.IDSong})');


解决方案

你也可以。是的。

您可以通过只包含这是值类型的属性一个简单的模型或字符串使用接受对象超载作为第三个参数

  @ Url.Action(搜索,搜索,模型)

你会想?号

在内部的方法将创建一个词典基于每个属性名称和的ToString()的价值和将其转换到查询字符串。不仅将产生的URL是丑陋的,如果你有很多的属性,或者属性的值包含长字符串,你可能会超过查询字符串限制,抛出异常。但主要的问题是,这是复杂的对象或集合任何属性将导致绑定失败,因为,例如,这是一个属性列表<串> 将产生 .. somePropertyName = System.Collections.Generic.List [字符串]&放大器; ....

只是传递模型的 ID 就像现在你做的事,在你的控制器存储库再次获取模式。

can I pass entire model as parameter to Url.Action or similar?

Actually I pass a parameter to the controller and I load the model, but I would pass entire model

window.location.replace('@Url.Action("Search", "Search", new { idSong = Model.IDSong })');

解决方案

Can you. Yes.

You can pass a simple model containing only properties which are values types or string using the overload that accepts object as the 3rd parameter

@Url.Action("Search", "Search", Model)

Would you want to? No.

Internally the method will create a Dictionary based on each properties name and .ToString() value and convert that to a query string. Not only will the resulting url be ugly, if you have a lot of properties, or the values of the properties contain long strings, you could exceed the query string limit and throw an exception. But the main issue is that any properties which are complex objects or collections will cause binding to fail because, for example, a property which is List<string> will generate ..?somePropertyName=System.Collections.Generic.List[string]&....

Pass just the model's ID as your doing now, and get the model again from the repository in your controller.

这篇关于MVC - 传递整个模型的参数Url.Action在JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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