Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
754 views
in Technique[技术] by (71.8m points)

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

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 })');
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...