I have a controller for my Portfolio, and the view model I am using is called DisplayItem. The function of the model is not relevant, but the models represent data about parts of my portfolio.
At the index page for this controller, the top 5 portfolio DisplayItems are built by pulling the required information from a database, and then a collection (List<>) is sent to the view. The view displays previews in a column with buttons to "view the project" for each project displayed. Clicking these buttons will route to the PortfolioController action "Display".
My question is about the efficiency of two methods of sending the appropriate data to the view for the action "Display". I can of course send the ID for the selected DisplayItem, and pull it from the database to rebuild the model and send it to the view. My initial thought was that this is unnecessary work for the database, seeing as I had previously pulled the required information. Though using the ID method would allow me to not pull the rest of the information until necessary.
My alternate idea is to serialize the model (it contains collections that can't be sent with POST), and then post what I need as a string to the action and then serialize into an object.
My experience is by and large not web, it is Game Programming, so I am out of my element to some degree and would love advice on which of the routes to opt for, or if there is some better way to do this.
I would prefer to query the database again whenever page loads. It allows to
-> check validation of input(id passed)
-> authorization of user for the requested info
-> decrease bandwidth(if data is large).
-> makes routing URL more user friendly(easy to bookmark).
As your application develops over time, your DisplayItem will have increasingly richer information pertaining to it, while your index view will only display summary information.
As the index view and the detail view will eventually require different sets of information, simply pass the ID field over the wire.
As Stepen Muecke says, reading from the database will be fast. Databases are good that way :-)
Related
I have a controller with a get and post action, on the post it checks if the model state is valid. If it is valid the page process the data and redirect. If the model state is not valid it will return the view back with the model. To ensure the drop downs have data I need to repopulate the items from the database which means I need to make another call to the database.
Is there any way to cut the call out to the database by caching or any other method?
The problem is that the browser is only submitting the values for your drop downs and not the text. You could get around this by creating a hidden element which submits the text in addition to the values.
But is that a good idea? In my opinion, no. You're creating extra network traffic between browser and the server in order to save traffic between the server and the database. In most cases it will be more efficient to retrieve the data from the database than the client.
Also, the data may have changed between when you sent it to the client and when you send it back the second time.
Ryan has a good point. I'm assuming you're coming from a WebForms background where everything was cached in the view state and posted back to the server. Asp.net MVC by its very architectural lends itself to a different approach where it's standard to re-query the database for data presentation to the user i.e. dropdown list values.
So you should only post back to the server the values the user has input. This is happening via the view-model. I would make a common method which accepts your view model and does the standard database pull and map to model. This way you can use the same method for the initial Get request and also for validation failure post-backs.
Also, if we're talking about small data sets then I would most definitely re-query as it's not expensive to do. If the drop down lists are huge then it depends on the data itself. Does this data change infrequently? Then it might be feasible to cache it on the web-server in a static list. If it does change frequently then you could look into more advanced solutions like memcached or redis.
I'm working on a calculator type app, three different views to perform three different types of calculations. For example purposes lets say it's just a multiplication table.
I want to be able to enter the factors, have the products update, then submit the view back to the model so I (hopefully) give it to a pdf converter (probably HiQPdf) and print it out that way.
So far I've gotten the view to show with some default values but when I submit everything it resets to those same default values. I realize this is cause by a serious gap in my knowledge of how the whole HTTP thing works, but any help with this would be really appreciated.
You need to save the data, at least in the Session, once the session ends, all data is lost, but while working in the site, is stays there. On the post action, you'll have to put the data in the Session[] and in your view you have to get the Session data and display it.
If the views just "repost" to themselves over and over as the user enters each new input you can create a ViewModel to hold all inputs, and just pass it back and forth, as opposed to 'storing' this data anywhere. (This could be as simple as a List, or you could make it much more intricate with the operation being performed, etc.)
Is there any third party tool or something in visual studio that lets you see the cached objects?
For example, an action is caching data (varied by parameters) and I want to see the cached objects and the attributes (like which parameter values were sent to the action when this data was cached).
You can find more answers to what you might desire on the Stackoverflow question:
How to display the content of asp.net cache?
Basically you can create a page to view all cached items from your application. After that, it can customize and UI pump it to your needs and objectives.
If you just need to debug, then you can use Ringer's solution displayed on your comments.
Is there a way to pass the complete model from the view back to the controller (without using JSON please)?
My model is a list e.g.
List<ExmapleClass>
I want to be able to pass it back to the controller, sort, then pass it back to the view to be displayed, so that I don't have to go back to the database to get the original data.
I guess having the list as a (class) member variable in the constructor will eliminate the need to pass the model back and forth, but do I have other options?
Conceptually within the servicing of a single request the communication from the controller to the view is one way. The controller decides on the view to be rendered, passes it a model, and execution never passes back to the controller.
You could execute a child action from within a view, which may achieve something similar to what you're after, but it's not clear based on your question.
If you're talking about communication that takes place across interaction with the user then you may be able to achieve something like this using TempData, where the view stores information in TempData to be consumed by the next controller that executes.
If your concern is performance based on having to repeatedly query a data source I would strongly advise you think about how to cache this data in a service or data access layer rather then try to use the view / controller interaction as a way of caching.
It can be done with hidden fields, like I posted here: Saving multiple records on submit click into differnt entities in MVC4. Not getting values from view in Controller, but if you don't need this data on view, caching is better solution.
Sending all the data back to the server will use a lot of bandwidth. I think it would be easier and faster to use JQuery/javascript and sort the data directly on the client side.
If you have to send the data to the server side you can XML but not sure if you gain anything by using XML.
Here are a few client side sorters:
http://tablesorter.com/docs/
http://www.sendesignz.com/index.php/jquery/76-how-to-sort-items-using-jquery
I have a data input module where I add the information of my product and its sub information like:
product basic info
product price info
product price details
price info and price details are related to product and are lists
In my web forms approach I would store my main product object on the view state and I would populate it's pricing info and details while doing ajax postbacks. This way I can create a compact module that is very user friendly in terms of defining a lot of data from one place without the need to enter these data from seperate modules. And when I am done I would do one product.save() and that would persist all the data to the respective tables on db.
Now I am building similar app on .net mvc framework and pondering on what would be the good way of handling this on mvc.
I don't resonate towards storing all this on client side till I click save. And saving to the db after each action makes me remember the days I was coding on asp.
Will appreciate your inputs on ways to approach this on mvc framework
I believe the best way of doing this is storing the data on the client side. It reduces unnecessary postbacks and improves responsiveness of your application. If you really want to store it on the server, you can use SessionState.
If you really want to store it in something like ViewState, you can go with a solution like this: ASP.NET MVC - Is there a way to simulate a ViewState?. However, I recommend against it as it will make things more complicated. Doing it client-side is probably the most elegant way and storing it in SessionState is the easiest.
Remember that you can always escape the MVC pattern and use a simple Web form for that specific page (which will give you ViewState where you need it): ASP.NET MVC controller actions design
store your Product list to the Model of the view and each time you change a value you can do a Ajax post to the controller and save the changes to the db, use partial views to display each item in your product list
you can try to integrate http://www.castleproject.org/ActiveRecord/ for easy saving and updating. That way you can just map your Model on your database using ORM(Object Relational Mapping). It takes a bit more work in the beginning but you will end up with simple commands like product.Update() and product.Create()