简体   繁体   English

MVC - 将整个模型作为参数传递给 javascript 中的 Url.Action

[英]MVC - Pass entire model as parameter to Url.Action in javascript

Can I pass the entire model as a parameter to Url.Action orsimilar?我可以将整个模型作为参数传递给 Url.Action 或类似的吗?

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

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

@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.在内部,该方法将根据每个属性名称和.ToString()值创建一个Dictionary并将其转换为查询字符串。 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.不仅生成的 url 会很丑,如果你有很多属性,或者属性的值包含长字符串,你可能会超出查询字符串限制并抛出异常。 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]&....但主要问题是任何作为复杂对象或集合的属性都会导致绑定失败,因为例如List<string>的属性将生成..?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.现在只传递模型的ID ,然后从控制器的存储库中再次获取模型。

You can try passing a ViewBag instead:您可以尝试传递一个 ViewBag:

Url.Action("Search", "Song", new { songId = ViewBag.SongId, songName = ViewBag.SongName})

Controller:控制器:

[HttpGet]
public PartialViewResult Search(string songId, string songName)
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM