I am using a Wizard control to capture users vehicles they own.
Stage 1 - Captures users name, address etc
Stage 2 - Captures only one vehicle (one vehicle consists of reg, model, mileage and manufacturer)
Stage 3 - Gives a summary of the details entered and saves the record when they click finish.
I would now like to capture multiple vehicles at stage 2.
Since the record is saved at stage 3 my guess is i need someway of storing each vehicle at stage 2 (which the user can view and edit if required before saving the record) and then passing all temporarily saved vehicles to my method at stage 3 to save it against the user.
How could i approach this? My concern with my research is that most of the methods available dont show how to store each vehicle temporarily?
You can use the Session variable to store the value if you are using the web application otherwise cache is also useful container. If your wizard is single page with multiple form you can also use the hidden field or viewState. Have a class with all the properties you want to store and use the class to put in the Session
public class MyData
{
public string Name;
public string Address;
}
In the first part of wizard do like this then once value is populated store in the Session before moving to next page. In next page so like this
MyData myData = (MyData]Session["myData"];
//again update rest of the data from current page and
Session["myData"] = myData;
//Go to next page
Related
A friend asked me to use Output Cache to cache data, that works good, but the thing is that I want to get data from the database and then interact with that data, I mean, I want to grab a huge data from the database, and then get only some data from that huge data according to the user activity dynamically.
[OutputCache(Duration =600, VaryByParam ="none")]
public ActionResult GetData()
{
var result = context.People.ToList();
return View(result);
}
That's an example, but let's say that I want to use pagination for that data in my view, and I want to show every person according to the date birth, for example default page current week, with a link called 'Next' and other one called 'Previous' . When 'Next' link is clicked I want to display only the people which birthday is the next week, same thing with 'Previous', showing just people with birthday on the previous week..
But using Output Cache I'm displaying the same data all the time... Is it possible to do this interacting with the database only the first time, caching that data, and then interacting with that data and not read against the database again during the time I set on the duration?? Do you advise me to use another Caching Tool different than Output Cache?
Instead of caching the entire view, maybe you could just add the result to the session state?
public ActionResult GetData()
{
if (HttpContext.Current.Session["peopleList"] != null)
{
return View((List<People>)HttpContext.Current.Session["peopleList"]);
}
else
{
var result = context.People.ToList();
HttpContext.Current.Session["peopleList"] = result;
return View(result);
}
}
You can control how long session state lasts in your Web.config or through IIS.
Starting at WebPage1, the user enters some data into a textbox and performs a search based on that search data. Then the user navigates away from WebPage1 to WebPage2 via a link.
How can I maintain their original search data when the user returns to WebPage1?
The user does not want to see the data in a query string. However the data is not sensitive, and any data from the client will be handled before processing.
We are using C# Mvc framework with Razor.
I have been trying to Post the entire model each time rather than using Get requests. However, this is not working well and does not follow a simple Post-Redirect-Get pattern more like Post-Redirect-Post.
You can use sessions to pass data from one webpage to another until you close the browser here is an example
//assuming the method below will return list of Products
var products=Db.GetProducts();
//Store the products to a session
Session["products"]=products;
//To get what you have stored to a session
var products=Session["products"] as List<Product>;
//to clear the session value
Session["products"]=null;
I am very new to MVC in general, so this might sound very silly. However, I don't quite get how to connect my data in the following scenario:
I am creating a project where I have basically two tables
Table 1
pid | pname | etc
Table 2
id | pid | etc
Now, I have successfully implemented a controller where I populate a list with the data in table 1 using "date range" as a parameter.
URL 1
/table1object?search=Search&endDate=2015-02-20&fromDate=2015-02-20
As the tables shown, each row in this view is connected to a certain row in table 2. So, for this I am generating a link which basically sends me to the details of the second controller by looping through the current model.
#Html.ActionLink("Details", "Details/" + #result.pId, "table2Object", null)
Now, my controller number 2 (Details) is only accepting an id to display that specific information of the Table 2.
URL 2
/table2object/Details/96
The Question
Since I am only passing this id to access the second controller, I am going to be able to see only one item at a time. What if I want to add a previous and next buttons to navigate through items in the second controller, but in the range established in the first controller? How can I do this?
Thank you
A first way to do it without respecting any best practices is to save your search result (Controller1) inside a Session variable.
Session["CurrentResult"] = YourViewModelInController1 ;
and then in Controller 2
ActionResult Details(int id)
{
var prevViewModel = Session["CurrentResult"] ;
// Do what you want to find the prev and the next from prevViewModel
}
Of course you can't get the next and the prev if you access your DetailController without displaying table1.
And also, there will be many side effects. e.g : when user make 2 search at once, ..
The second good and correct way to do it, is to re-execute a search to find the previous and the next on Controller 2 Details. Independently of the previous
ActionResult Details(int id)
{
// Do a real search to find the next and the previous
}
Considering you have the idea how to pass values and work in a controller, i can give you a suggestion as below:
You can load your items in a list within a given date range first. Pass that list to 2nd controller And then traverse that list by clicking prev and next using a counter.
Hope this helps,
Thanks
I am developing an application in ASP.NET using C#. In my code I am creating multiple sessions. In the next page I want to get all the session value which names starts with a particular string. How can I do that?
Suppose in my page I am creating 5 sessions with session name gs_text1 to gs_text5 and creating another 10 sessions with name like es_label1 to es_label10. Now on the next page I want to add some value to all the sessions which name starts with es. How can I do that in ASP.NET?
An HttpSessionState object has a property on it called Keys which will return the keys for all the items you've put in session. You can search these keys with linq or something to get all the keys that start with a given value. YOu can then loop through them and do whatever it is that you want to do.
var requiredKeys = Session.Keys.Where(x=>x.StartsWith("es"));
foreach (var key in requiredKeys)
{
//Do Stuff here
}
N.B. When you say you are creating "5 sessions with session name [blah]" I assume you mean you are putting objects into the session with those names as keys. If this isn't what you mean then you might need to go back and explain why you are creating multiple sessions so we can understand better...
I'm creating a maker-checker functionality where, maker creates a record and it is reviewed by the checker. The record creation will not happen until the checker approves it. If the checker rejects it then it should be rolled back.
The idea which i know is create a temporary table of records which will hold the records to be created until the checker approves it. But this method will have 2X number of tables to be created to cover all functionalities.
What are the best practices to achieve this?
Is it something which can be done using Windows Work Flow.? (WWF)
Just add a few more columns to your db.
Status : Current status of record ( Waiting for approval, Approved,
Declined . etc)
Maker : Username or id
Checker : Username or id
Checker Approval/Rejection Time
Alternatively if you have lots of tables like this needs to maker/checker workflow you can add another table and reference all other record to this table.
Windows workflow foundation also could work for you but i persnoally find it difficult to use
If you want revisions for the recored you also need more columns. Such as Revision Number and IsLastRevision.
I dont know what you are using for accessing db and modifiying records. If you are using OR/M you might override Update and do revisions on all saves . For Example
void Update(Entity e )
{
Entity n = new Entity();
// Create a copy of e ( With AutoMapper for example or manually )
e.Current = true;
e.RevisionNumber += 1;
Update(e);
Insert(n);
}
In this case you will have two options:
Create two identical tables and use one table for approved data and one for requested data.
OR
You can create two rows for a user with TypeID(requested/approved). in this case user will create a request with typeID = requested when approver approved that request you simply override current approved record with new one otherwise simply mark requested row with rejected status.