mvc and jquery - does the model get left out? [closed]
I am building an asp.net mvc 3 web application. I am also a jquery fan. I am not sure how to go about the design, as currently I can see two options:
Use the controller as something similar to a web service. Encapsulate the method calls relevant for each page, and use jquery to perform ajax calls to the web page. Then, use jquery tempting to populate the page based on the server's response.
Just use the model in my view. Avoid any jquery ajax calls that have to do with populating page's content.
I guess that there's no right or wrong here, but I'd really like to hear the pros and cons of the two options from experienced developers.
Thank you
it depends upon situation to situation. look at stackoverflow for instance. it is built in asp.net mvc AFAIK. it serves major content in traditional way whereas adding comments, showing them simultaneously, tag selection is done using ajax calls. we have been using same approach: serve major content(questions and answers on stackoverflow) in traditional way and do minor tweaking with ajax and jquery
I think it's largely dependent on the model size and personal preference. If you have a large model, you'd almost certainly want to use AJAX so you don't delay the page from opening.
It's easy to populate a dom object with some data from the model. It's a little more difficult using that data within javascript though. You either have to use things like hidden inputs, or write a html helper that serializes your model into javascript and loads it into a javascript object. The downside to this approach is that your model data is clearly visible to anyone who looks at the code behind and it makes your page size larger.
I've been using approach number one personally.
精彩评论