Asp.net mvc displaying results about a selection being made - asp.net-mvc

I am porting an application from wpf to asp.net mvc.
In the wpf I have a view in which the user selects from a combobox the name of a client and then in some textboxes, next to the combobox, some specific information about the client will show up (email, address, etc). In wpf I fill all this information in an observablecollection that resides in memory and when the client changes, I retrieve the other ones from the collection so it won't fo through the database.
Is there any way in asp.net mvc I can do this ? Or every time the client changes i will fetch from the database the extra information ? (will be slow)
This controls is just to select the client (and the extra information to help the user) so it should be fast.
How would you do it ?

If you don't want to make a call back to the server for every selection, then you should look at a solution whereby you pre-fetch all the data that you need and hold it in a javascript object. Then you could write a javascript method that would fire on the selection DOM event.

Sounds like what you are looking for is Session. You can pull the list from the database once, store it in session, and then reference session as needed for each client's data. This recommendation is based on presumption that the list is not being modified elsewhere, and therefore, invalidating the version of the list you have stored in Session.
Using Session in ASP.NET - http://msdn.microsoft.com/en-us/library/ms178581.aspx

Related

"State" management for asp.net mvc multi partial view + ajax app

I am trying to convert my asp.net mvc4 app, which had fairly heavy use of SessionState, into a stateless app. I understand that I can store this information in the DB, and intend to do so.
My question, though, is about my particular architecture. My app has a main 'page' consisting of a number of partial view panels, which each have actions in them that can affect the other panels. What i've been doing up to now is storing the entire state of the viewModel (lots of inter-related EF list collections and 'record' objects) in the session, and its been working great. Except when the session just randomly dies.
So, I need to get this data out of the session, and into the DB where I can rebuild the thing at need. My concern is that, if I store the info in the database, every single action done on screen might affect 3-5 different panels, each with their own State updates, thats a minimum of 10 round trips to the DB for every interaction!
What are some strategies I can use to make this idea more scalable?
EXTRA INFO
The view in question here is a sort of POS shopping cart system. There are panels for selecting events, selecting/adding items to the cart, editing cart items, selecting contacts, editing contacts, displaying the cart items, displaying the cart 'subtotals', and finally, a panel with a [checkout] button.
Selecting a new event will change the list of available items. Selecting an item to add to the cart will change the cart item list, subtotals, as well as the checkout panel. Same for editing a cart item.
The main concern is how to recover from a lost session, as I've found the built-in asp.net session code too unreliable. My testers have encountered issues with sessions timing out, and then my app not having any kind of recovery process. When its installed on 1500 sites, each with an average of 10 users, its going to be a plague of lost session issues, and I need to combat that before it becomes a real problem.
I agree that I'm not going stateless...wrong choice of words used in a rush. I'm just trying to move that state into a form that I can rely on past the session failure. My main idea presently is to continue using the session as the local cache for the viewModel data, but to have a fallback operation that can rebuild the viewModel from DB if the session one is lost somehow.
You shouldn't necessarily be using a database to store (what sounds like) data that only needs to be persisted in the short term.
If these changes to the other partials are only relevant in the context of the current "master view," then I would suggest using jQuery AJAX to send off the requests, parse the response JSON and update the other views. Tutorials on jQuery AJAX and ASP.NET MVC are easy to find, if you don't already have the knowledge:
http://www.codeproject.com/Articles/41828/JQuery-AJAX-with-ASP-NET-MVC
This way, you don't need to make a bunch of round trips. If the changes need to be persisted beyond the context of the current view, make ONE round trip to the database to perform the update and then simply update all of the other partials from the in-memory response from the AJAX call.
You don't need to read from secondary storage multiple times when you already have all of the information you need in-memory. Just do the reading and writing once.
I decided to go with a hybrid approach. I'm still using session, but I'm building out a DB 'recovery' option, so that if the session portion is lost, the DB will be able to provide the values needed to rebuild the session seamlessly.
Seems to be working well, so far.

How can I bind data to dropdown on every postback?

In Asp.Net MVC, getting data from db and binding into the dropdown. On postback of the page, dropdown items becomes empty. Every postback need to hit the db and bind the dropdown ?
Please suggest.
Web requests (regardless of its technology, MVC, Webforms, etc.) are stateless. So, everytime a new request (of a "full postback" as you call it) occures, you've lost the state of the previous request.
So, yes, you need to reload the data from the database and rebind the Dropdown, but without your code, it's hard to tell you how and when to do it. However, a number of thing can be done to prevent a database hit. Choose what suits your need:
AJAX request to refresh only some part of the web page you need;
Memory caching on the server (using a cache service or output caching even);
Using sessions (not recommended);

How to persist a model across multiple requests in ASP.NET MVC 2

I'm building a web application that has a particular model representing some events. Users need to be able to add N number of people to a given event. Choosing people is handled by a partial view.
I'm trying to build a menu that displays when users click "add a person" to the event. Because the event hasn't been filled out completely yet, there is nothing in the database to persist between requests.
I also have validation logic on the event page.
My proposed solution is to add the form to search or add for people on the event form itself and have a submit button that sends the values that have been added back to the server, where I can store them in ViewData or Session.
Unfortunately, doing this flags the validation.
My second solution is to load a partial view responsible for loading the UI to add/search for a person. I could add a little code on the method in the controller that returns a partial view storing the existing data in a session variable or viewdata. Trouble is, I have to submit the form to do it--again tripping the validation!!!
I'm wondering if perhaps I chose the wrong tool to do this...because in webforms, there would probably be a postback and you would just perform an operation on that postback. I'd like to avoid rewriting the application in webforms and am wondering if there are ways I'm overlooking in ASP.NET MVC.
Thanks in advance for the ideas!
I would probably have the partial view send it's data to the main page (with javascript). That way there is only one post to the server and it is when all of the data the user needs to enter has been filled out. How are you displaying the partial view? Is it on the main page (in a div), or is it a separate pop-up window? Either way, you should be able to use javascript to store this data on the main page and post all of the data back at one time.
HTH

How to store user preferences? Cookie becomes bigger

My application (Asp.Net MVC) has great interaction with the user interface (jQuery/js). For example, setting various searches charts, moving the gadgets on the screen and more .. I of course want to keep all data for each user. So that data will be available from any page in the Dumaine and the user will accepts his preferences.
Now I keep all data in a cookie because it did not seem logical asynchronous access to the server each time the user changes something and thet happens a lot.When the user logout from the application I save the cookie to the database.
The Q is how to save the settings back to the db - from the client to the server.
because the are a lot of interactin that I want to record.
example scanrios: closing widget,moving widget,resizing menues, ordering columens..
I want to record that actions. if I will fire ajax saving rutine for each action
ןt will be too cumbersome. Maybe I have no choice..
Maybe I should run an asynchronous saving all of a certain interval seconds.
The problem is the cookie becomes very large. The thought that this huge cookie is attached to each server request makes me feel that my attitude is wrong.
Another problem cookies have size limit. It varies from your browser but I definitely have been close to the border - my cookie easily become 4kb
Is there another solution?
Without knowing your code, have you considered storing the users preferences in a/your database. A UserPreference table with columns for various settings is a possibility.
You could update it via AJAX/JSON if you had a 'Save Preferences' option, or just update it on postback.
EDIT 1: After thinking about it, I think having an explicit 'save preferences' button would be beneficial and practical.
Somewhere on your page, where the use edits the things that generate the cookie, put an button called save, then hook up a jQuery click handler. On click, build a CSV string or another method of storing the preferences for posting back to the server, then use $.post to send it back to an action method in a controller.
Once there, store it in the database somehow (up to you exactly how), then return a JSON array with a success attribute, to denote whether the preference storing was successful.
When the page is loading, get the preferences out of the database and perform you manipulation.
Another solution would be to store the user preferences into the session and write some server side logic (like action filter) that would write those preferences as JSON encoded string on each page (in a script tag towards the end of the markup) making them available to client scripts.

How should I obtain complete list contents after a post?

using asp.net mvc, I'm initializing a list in the server code, and allowing the end-user to add to the list on the form (using JQuery to add entries). I know how to obtain a list's selected items on a post back, but I don't need to do that here. I want the complete contents of the list accessible in the server code after a post back.
Is a posted list just not going to give me the full content? Should I use Ajax to send each item to the server as each items gets added to the list?
thanks
There are a couple of ways that I can think of doing this.
make an ajax call each time an item is added - as you suggested
when an item is added to the list, you also add a hidden field to the form that will be submitted. Then on post back (although that terminology is very Web Forms-y), in your action method, you'll have access to the contents of the list. If you name things correctly you should be able to model bind to a List.
The latter would be my preference, it depends on your particular situation though.
Some things to keep in mind:
Bandwidth from the user to the web server is very small compared to bandwidth from a database to the web server
The database most likely cached whatever query you just ran to populate that list
It sounds like you're asking for the entire list to be included with the postback data, and that means having the browser upload that data for the user.
With all that in mind, it should be obvious that you're better off rebuilding your base list from the database.

Resources