MVC - 将整个模型作为参数传递给 javascript 中的 Url.Action [英] MVC - Pass entire model as parameter to Url.Action in javascript

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

问题描述

我可以将整个模型作为参数传递给 Url.Action 或类似的吗?

Can I pass the entire model as a parameter to Url.Action orsimilar?

实际上,我将参数传递给控制器​​并加载模型,但我想传递整个模型.

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

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

推荐答案

你能吗.是的.

您可以使用接受 object 作为第三个参数的重载传递一个仅包含值类型或 string 属性的简单模型

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)

你愿意吗?没有.

在内部,该方法将根据每个属性名称和 .ToString() 值创建一个 Dictionary 并将其转换为查询字符串.不仅生成的 url 会很丑,如果你有很多属性,或者属性的值包含长字符串,你可能会超出查询字符串限制并抛出异常.但主要问题是任何作为复杂对象或集合的属性都会导致绑定失败,因为例如,List 的属性将生成 ..?somePropertyName=System.Collections.Generic.List[string]&....

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]&....

像现在一样只传递模型的ID,然后从控制器的存储库中再次获取模型.

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

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

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