Populating data based on users selection - c#

How do you populate data coming from SQL server based on users request?
Example:
I have a page with about 50 links(all user links) and I want to be able to have the user click on their link and bring another page(template I believe) with all their data. The catch here is that I don't want to create 50 pages(one of each user).
How can I accomplish this in ASP.NET C#?

Its simple as creating an aspx page and a model.
Profile.aspx - this page contains all the view display for the current user or the requesting user. You can pass the user id through the link you had. Example:
John Doe
Now in the page load or the controller (MVC) - code behind you can use that id to retrieve the details of the user and start to populate to your aspx page. Example:
txtName.Text = UserModel.Name
Or if your are using MVC for the web application it will be easy as naming the controls and setup the model in your view aspx.

I think this site could get you started: http://www.asp.net/mvc/tutorials
There should be very similar examples to what you would like to do. Explaining it here would just duplicate existing good material.
If you are completely new to programming, try a C# tutorial first, for example: http://www.csharp-station.com/Tutorials/Lesson01.aspx

Related

Re-rending the C# MVC Action using JS

I am using C# MVC for my project.
In Home page, I am rending one action using razor syntax.
Index.chtml Page
<div>
#Html.RenderAction("OrganizationManagerData","Home")
</div>
When I first time load Index page, "OrganizationManagerData" method also render and it shows employee data with all who are working under him/her in Tabular format.
Logged In manager can do many things in that like provide feedback to individual, give task, submit performance report of individual to higher authority, so on.
We have one more option on Index Page "Filter" when Manager clicks on it, it will show popup and can select the filter which needs like good performer, avg Performer, by task, so on.
What I want to achieve is when manager select Filters and click Apply, I want to re-render the table so that it will display filtered data.
What I am thinking is When user will click on "Apply", will call again DB and will show filtered Data
Is it good approach however I don't know how to re-render the "OrganizationManagerData" action again on Apply Click?
Or
Is it any better way to do it?
What is usually best practice to display intuitive dashboard which have loads of data.
Would like to hear your opinion.
Thanks in advance.
I just thought of maybe I will call Ajax for OrganizationManagerData and it will return json format which I will use to format table in JS.
However for this have to write lots of html in Javascript.
I have hide the rows which are not matching with my filter condition using JavaScript.

Passing data between Views and Controller

Wanted to see if somebody can provide some suggestions/pointers on how to address this issue I am currently facing
.
Scenario is likeā€¦
I have a Page say A where search is performed and data is displayed.
On Page A there is an ActionLink which passes an ID of the selected record to the page B.
Before redirecting to page B, I need to store/preserve form data of Page A (I do not want to use session or hidden filed as data is critical).
Once user is done working on Page B, I need to save changes on Page B and redirect back to Page A.
Here Page A should make a Post request based on preserver form data in step 3. Purpose is to display back searched data that was there before redirecting to Page B.
Something like..
View A
// Search textboxes here
// Search data list here with ActionLink column
View B
// more controls here
// Submit button - saves changes on this page.
// after submit button processing need to go back to View A
// and display the same Search data List based on Seach textboxes values
// those were entered by user before coming to this View B.
// Ques: Not sure how to persist data from View A between calls to View B and then View B to View A.
Does anybody has any better approach to achieve this?
For now the solution I have is...
Make an ajax POST request on ActionLink click and save the
formcollection in cache using controller.
Make default ActionLink GET request passing ID and in controller
return View B.
On View B, on submit do ajax POST request to save data on Page B and
return data from cache in ajax success function.
Make another ajax POST request using data retruned in above ajax
success to display View A.
Thanks in advance
If you're relying on full page refreshes then you need to use Session. You didn't give a reason as to why you don't want to use it, but Cache object is totally inappropriate for this purpose as it can be cleared out if the server needs to regain some memory, as well it is shared between users. Session is specifically built for the purpose you're describing - preserving data between full page refreshes.
Alternatively I would look into building your site as a Single Page Application, aka, you don't do full page refreshes between navigation, and can store data in a javascript object on the client. Due to the fact you put a lot of emphasis that the data is critical, the Session might be safer though as your javascript objects will get cleared out if the user accidentally navigates away from the page, where is a server based session object will preserve it for the duration of the session lifecycle.
You can also store the data in a cookie/local storage object (html5), but this is probably overkill for what you're doing.

How to create user generated pages in asp.net C# from form

I am new to asp.net C#. How can I create a form where a user can fill out a couple of text fields (page name for instance) and then create a new page on submit. Then they would be able to select the page from a select box and go to it to add content.
I don't know how to implement this and I do not need a full on CMS.
You can create the form to enter the values into a sql table. so, for instance you'd have a title text box, a content text box, and then a button. In your button action you will enter that data to the table.
then you can create a navigation that for intance - to keep it very simple - does a select all titles from table name. you'll have these titles as your pages. so, upon clicking the title you'd have a script to bring up the details.
This is a very simple overview of how you can create pages.
since you said you are new I'd recommend going through some tutorials. I highly recommend plural sight http://www.pluralsight-training.net/microsoft/
hope this helps!

Pre-populate model value on MVC3 page

I am new to ASP MVC3 and I am trying to make a set of pages for creating and editing sales notes records for customers in our database. On the Customer detail page, I have a table that will display all sales notes for the selected customer and it has a "Create Note" button that goes to a create page for sales notes. How do I pass and pre-populate the CustomerID field on the note with the ID of the customer from the previous page? I searched for an example or tutorial that had this scenario but didn't find one. If you know of a public example a link to it or a direct answer to the question would be greatly appreciated.
You can accomplish this fairly easily with routing parameters. First you need to define the route for the create note action that takes a customer Id. Next, you need to create the action so that it accepts the parameter.
A quick Google search or "ASP.NET MVC Routing Parameters" turned up this URL which explains it very well:
http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs

Saving data on a web page when switching from one page to another in ASP.NET?

I'm new to ASP.NET. I'm designing a user interface in Asp.NET and C# where the user can login and then launch an application. When using this application the user has to fill out a form that is 10 pages long.
So, I have used navigation menu and designed the interface in such a way where every page is different menu item and it is a static menu. The user fills out the details on the first page of the form and saves it and the data gets saved in the database.
The problem is he moves to the other page by clicking the menu tab; when he comes back to the first page by using the menu tab for that page all the filled in data is gone and he sees a blank page. I know that is how it works but I want it in such a way that in one sitting when he is filling out the data on the second page (after filling the data on first page) on reverting back to the first page he should be able to see the data that he had filled out.
What concept can I use? I'm not sure view state will be helpful in this scenario.
You should look into using the Session State variable for storing his information over the entire session. If the user is logged in you should think about storing his information that he enters in a database and having a Boolean state of "ApplicationFinished" to check if he has finished it or not. Otherwise I would have a call on each page to retrieve information from the database that has already been added, so that he can fill out information at different sittings or all at once.
http://msdn.microsoft.com/en-us/library/ms178581.aspx
Session State may be too long term for you, and if that is the case do some research on ViewState. There are a lot of different ways to tackle a problem like this. It all depends on which technology will fit your needs the best.
http://msdn.microsoft.com/en-us/library/ms972976.aspx
Also, if you're using a tab system think about using the AJAX tabs so that the data will remain on the forms even while tabbing through the different tabs.
http://www.dynamicdrive.com/dynamicindex17/ajaxtabscontent/
Well, if you are write the data on database, i guess the best (fast) workaround is to add a column named "completed" to the table the hold this informations. If the flag "completed" is not setted to 1, you load the database information and fills the page controls.
When you create a new record in the database, get the ID of the record and set it on Session. If the user gets back to the first page (previous page), you can recover the information ID and load the data.
As long as you are learning new things... add jquery to the list and leverage the JQuery Wizard Plug In. It will allow you to integrate your "10 page form" into a single unit, which can then be sub divided, routed and managed more easily. You can leverage AJAX to save each step, while offering built in navigation and validation methods.
I would suggest that you switch to using client-side javascript to control your tabs. That way your form data stays in the fields when you switch back and forth between tabs. You can use javascript buttons to guide the user from tab to tab.
I've done this using JQuery. I actually had 150 fields that needed to be captured (they were all required). I group like data on different tabs and then had buttons ('< Previous', 'Next >') which would activate different tabs.
When they are done, then display the 'Save' button.
This not be what you are looking for, but if your problem is that you want all of the input of a previously filled page to show up when a user navigates back to it, and you have already saved all that information, then you can try something like this:
HTML
<input type="text" id="yourID" name = "yourName" value = "<%=data%>"/>
Then all you need to do is set data to public in the code behind. Then to get the value for data just make a call to your database.
Make sure that you make data empty on the init call public string data = ""; or whatever type it is. This way if there is no info, then it will be blank, and if there is saved info, then it will be filled in.
You can also attempt to pass all the data through params in the url like so:
C#
Response.Redirect("webpage.aspx?data=" + data + "&data1=" + data1);
Or though javascript:
window.location = ("webpage.aspx?data=" + data + "&data1=" + data1);
To get the request do this:
if (Request.Params.AllKeys.Contains("data"))
{
data = Request.Params["data"];
}
This way is less ideal though if there is a lot of data being passed.

Categories