I'm hoping this isn't too subjective, but I'm new to ASP.NET MVC and I'm trying to figure out how others may have solved similar problems.
Basically, I have two entities, Customers and Orders. A customer has many orders; an order belongs to exactly one customer. I'm making an Order Search feature that should allow a user to search for orders based on order or customer information. Pretty straightforward, I think.
I've read in other posts that the search controller should use GET, but I think it makes more sense to use POST because of the large number of search params.
I'm using Entity Framework to create my models, and that's in a separate class library project and namespace.
This article talks about using binding instead of Request.Form to get at the POST data. Would it make decent sense to make a class to hold all the search data that could be materialized by the magic model binding? Otherwise I'd just be poking through the FormCollection to pull out particular values, which mi开发者_运维问答ght be fine. Where would you recommend making such a class?
This is only a partial answer, specifically to the "FormCollection vs Class" part. It's my opinion that you should always use a class for this unless you have a very good reason not to. You get compile-time checking here which is the #1 benefit. You also get Intellisense support which is helpful too. Lastly, you might get some performance benefits as there is potentially less casting/parsing done by your code.
For the GET vs POST question, I'm still struggling with that general question myself but I do have an opinion on your specific use of it. Currently, I'm leaning towards the following rules:
- Use GET if the parameters identify an
entity
of some sort (i.e. ~/product/id/54 = a can of Coke) - Use POST if the parameters help generate a truly dynamic page where an ungodly number of such pages can exist (a search results screen being an example where there is nearly an infinite number of possibilities).
Now I may be way off with my GET vs POST opinion here, but I think there will be a lot of agreement on the class vs FormCollection opinion.
Use a class to encapsulate the search criteria. You can make this a property of your model and then use standard model binding. That way you can pass a single object to your search method, which is much neater and more extensible than having lots of parameters.
精彩评论