How to store my data using at the end of my step in MVC? - asp.net-mvc

I have a form using a Table of REQUEST Using IDRequest (int), TitleOfRequest
and other Table events which can have multiple events for the same request.
So the Table of events will include :
IDEVENTST,
IDRequest,
IDTypeofEvent,
DateStstrong textart,
DateEnd
Table of TYPEOfEvent : IdTypeOfEvent,TitleOfType
From my view to create a new record, the record will create at the final step, i want to make it in order
1 ) Enter a form with fields TitleOfRequest
And fields where the person can enter : DateStart and DateEnd, TypeOfEvent and a button to add, and load the temporary records in datatable.
My questions is : what is the best way to store this temporary data with MVC ? And refresh it in my view then at least to save in my database.

You could use [TempData] if you want to store data for the next request only.TempData allow us to persisting data for the duration of single subsequent request.
If data should be accessible between multiple requests, then use Session.
[Session] is able to store data much more long time, until user session is not expire.
Here is a good blogpost on this matter:
http://petermcintyre.com/2013/01/27/asp-net-mvc-data-persistence-choices/

Related

What is the difference between new sap.ui.model.odata.ODataModel and read?

I am playing around with a OData service and I am very confused when to use this
var oModel = new sap.ui.model.odata.ODataModel("proxy/http/services.odata.org/V3/(S(k42qhed3hw4zgjxfnhivnmes))/OData/OData.svc");
this.getView().setModel(oModel);
vs
var oModel = new sap.ui.model.odata.ODataModel("odatserviceurl", true);
var productsModel = new JSONModel();
oModel.read("/Products",
null,
null,
false,
function _OnSuccess(oData, response) {
var data = { "ProductCollection" : oData.results };
productsModel.setData(data);
},
function _OnError(error) {
console.log(error);
}
);
this.getView().setModel(productsModel);
I have two working example using both approach but I am not able to figure out why using read method if I can achieve same with first version. Please explain or guide me to the documentation which can clear my confusion.
Ok, lets start with the models:
JSON Model : The JSON model is a client-side model and, therefore, intended for small datasets, which are completely available on the client. The JSON model supports two-way binding. NOTE: no server side call is made on filtering, searching, refresh.
OData Model : The OData model is a server-side model: the dataset is only available on the server and the client only knows the currently visible rows and fields. This also means that sorting and filtering on the client is not possible. For this, the client has to send a request to the server. Meaning searching/filtering calls odata service again.
Now, lets look at scenarios where we will use these models:
Scenario 1: Showing data to user in a list/table/display form. Data manipulation is limited to searching and filtering. Here, I would use oData model directly to controls as only fetching of data is required.( your method 1) (NOTE: One way binding). Remember here all changes require a call to server.
Scenario 2: I have an application which has multiple inputs, user can edit changes, also some fields are calculated and mandatory. All in all, many user changes are done which may be temporary and user might not want to save them. Here, you dont want to send these temporary changes to backend as yet. You way want to manipulate, validate data before sending. Here, we will use JSON Model after reading data from odata model ( your method 2). Store the changes in local JSON model, validate and manipulate them and finally send the data using Odata create/update. Remember here all changes DO NOT require a call to server as data is present in local JSON MODEL.
Let me know if this helps you. :)
EDIT : Additional Information :
As per your comment :
Documentation says oModel.read' trigger get request but new sap.ui.model.odata.ODataModel("proxy/http/services.odata.org‌​/V3/(S(k42qhed3hw4zg‌​jxfnhivnmes))/OData/‌​OData.svc")` does the same thing so why and when to use oModel.read
Here, is where you misunderstood. The code
new sap.ui.model.odata.ODataModel("proxy/http/services.odata.org‌​/V3/(S(k42qhed3hw4zg‌​jxfnhivnmes))/OData/‌​OData.svc") will NOT send a read/get Request. It calls the odata services and fetches the metadata of the service. A service can have multiple entities.
For example: the service :http://services.odata.org/Northwind/Northwind.svc/ has mutiple entity sets such as Categories, Customers, Employees etc. So, when I declare : new sap.ui.model.odata.ODataModel("http://services.odata.org/Northwind/Northwind.svc/") it will fetch the metadata for service (not actual data). Only when you call the desired entity set, it will fetch the data. The Entity set is specified :
When you call the read method ( like you have specified '/Products')
Bind the entity set name directly to control like to List,Table etc ( items='{/Products}' )

CloudKit : How to handle account changes with local persistent store?

In my application i have to maintain a local persistent store in sync with cloud kit private database. So I just wanted to know how can I handle account changes that may happen.
Confusion I have is as below:
say a set of records belong to user A now if user B log's in to the same phone I can do the following of the 2 things:
Ignore user and let data sync to B account too but that way A's data will get sync to B's private account too. Here the record change tag and all get a bit mess up since am saving CKRecord encoded fields to database.
I can maintain a user table and link each record to the user that is logged in that way user data will get separated. So should I maintain a user field along with all records ?
How can this be best handled even apart from above 2 things.
Of course in your local persistence store you could add the userID to personalize all records. An other mechanism is to remove all local data and fetch the users data when a change is detected. If you want to keep the users data on the device you could also create separate data stores for each user.
You can detect a changed login by adding the following code in your app delegate or root view controller:
NSNotificationCenter.defaultCenter().addObserverForName(NSUbiquityIdentityDidChangeNotification, object: nil, queue: nil) { _ in
/// remove local data and fetch user data
}
You should also refresh all user related data in memory and refresh the loaded views.

What is the best way to save data online from UITableView?

My application
UITableView has 200 rows
In edit mode each cell has two actions for eg: passAction and failAction
After editing i want to update the database with either 0 or 1 based on its selection
I am retrieving data from server as json and storing it as object
Which is Best?
a. Requesting the server on each time the action is called.
b. Storing it in the local database and sync on completing all rows.
c. Request the server once on completion and send the whole object as JSON.
Help me in choosing the best option of implementation I can do!
Correct me if I bypassed any rules of SO because this is my first question!
#Dan Beaulieu: No need to send all data to server after edit. First update your database and Just send data which was changed in your database.
So add one field like "sync" in your database table and updated field set sync = 1 during edit and get data from database which was set sync = 1 and send it to server.

Get previous value of an entity

I need to get previous value of an entity.
My requirement is like; I have some input fields in an edit page.
1 User can enter some values there and press save button at this time the user should be able to save it.
2 User can enter some values there and press Cancel button at this time the page should be reloaded with whatever values were there before the user start editing the page.
My question is that can entity frame work, help us in getting previous value of an object?
Is self tracking is something related to this?
You mentioned "page" so I guess you are talking about web application. In such case you should simply load entity from the database again because pushing Cancel button will make a new request to your web application. You should use a new context per request so you don't have any previous data or entity to reload - you will run a new query and get last data persisted to database.
What you would want to do is:
myContext.Refresh(RefreshMode.StoreWins, myObject);
This will ask the context to reload the entity removing any changes to the object and replacing the property values from the data store.

How can I store user information in MVC between requests

I have an MVC2-site using Windows authentication.
When the user requests a page I pull some user information from the database. The class I retrieve is a Person class.
How can get this from the database when the user enters the site, and pick up the same class without touching the db on all subsequent page requests?
I must admit, I am pretty lost when it comes to session handling in ASP.net MVC.
You can store that kind of information in HttpContextBase.Session.
One option is to retrieve the Person object from your database on the first hit and store it in System.Web.HttpContext.Current.Cache, this will allow extremely fast access and your Person data will be temporarily stored in RAM on the web server.
But be careful: If you are storing significantly large amount of user data in this way, you could eat up a lot of memory. Nevertheless, this will be perfectly fine if you only need to cache a few thousand or so. Clearly, it depends upon how many users you expect to be using your app.
You could add like this:
private void CachePersonData (Person data, string storageKey)
{
if (HttpContext.Current.Cache[storageKey] == null)
{
HttpContext.Current.Cache.Add(storageKey,
data,
null,
Cache.NoAbsoluteExpiration,
TimeSpan.FromDays(1),
CacheItemPriority.High,
null);
}
}
... and retrieve like this:
// Grab data from the cache
Person p = HttpContext.Current.Cache[storageKey];
Don't forget that the object returned from the cache could be null, so you should check for this and load from the database as necessary (then cache).
First of all, if you are using a load balanced environment, I wouldn't recommend any solution that you try without storing it in a database, because it will eventually fail.
If you are not in a load balancing environment, you can use TempData to store your object and then retrieve it in the subsequent request.
HttpContext.Current.Session[key];

Resources