最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net mvc 5 - MVC - Pass entire model as parameter to Url.Action in javascript - Stack Overflow

programmeradmin1浏览0评论

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

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 })');
Share Improve this question edited Jun 25, 2019 at 19:27 live-love 52.4k26 gold badges251 silver badges215 bronze badges asked Feb 5, 2016 at 8:22 M0rDoKM0rDoK 1321 gold badge1 silver badge10 bronze badges 2
  • 3 What are you trying to achieve? – Arnaud Weil Commented Feb 5, 2016 at 8:24
  • To send object by using ajax without jquery – malik masis Commented Aug 12, 2021 at 12:07
Add a comment  | 

2 Answers 2

Reset to default 16

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.

You can try passing a ViewBag instead:

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

Controller:

[HttpGet]
public PartialViewResult Search(string songId, string songName)
{
}
发布评论

评论列表(0)

  1. 暂无评论