Multiselection and different actions - c#

I am working in a project that have a Person model. I display the Persons in an Index action with a table, showing Checkboxes allowing to select multiple people.
The user is supposed to be able to do different things to the selected people, like:
Approving them for access.
Sending an email message.
The action of approving them for access is just making a database change. The action of "Sending an email" should redirect to another page that would request the details of the email so it can be send.
So far we have a table inside a Form that have the people. That form is posting to one action and I am able to retrieve the selected IDs. So far so good.
My issue is that I would like the form to post to different routes dynamically (if the user clicks on the action to "approve" the user to go to one place, if the user clicks on the actions to "send email" to go to another place).
I haven't been able to find how to dynamically modify the form action to post to a different Url on runtime. Is there any way to do that? (or maybe I am doing all this wrong, and I should, somehow, collect the IDs with jQuery and post to the Urls that I want).

What you need to do is to change (on the client, using javascript) "action" attribute of the form element:
var url = ...
$("form").attr("action", url);

Related

Large number of forms on page

I'm using ASP.NET MVC to create a web application and have stumbled upon a situation where I'm not sure what the best solution would be.
I have a users page where I am simply loading a list of users and their roles into a table. The roles are checkboxes, for example Manager and Administrator are two separate roles that are expressed as checkboxes on that row for the user.
In the far right column is an Actions field with 3 buttons: Save, Change Password and Delete.
Change Password works by opening a dialog box and hitting an action on the Controller called ChangePassword, this is a Post request as the form is in a different area and some values are set to hidden fields using JavaScript.
Delete works by calling an action called Delete which is a Get request where in the view the action parameters are explicitly set by using the values from the Model. Since these values aren't going to change I can safely do something like this:
#Url.Action("Delete", new { loginName = Model.Users[i].LoginName, username = Model.Users[i].Username })
However, the Save action is different because the save action is meant to be used to update the user roles, which the user can change by selecting different checkboxes. My initial plan was to make the Save action a Get request and have the values passed in similar to how I do this with Delete but I would need to set some sort of hidden field using JavaScript so that I know which fields are updated.
An alternative solution would be to have every table row a form and make Save a Post action and have the values from the checkboxes bound to the model on the action. I'm not sure whether this would be advised because this could potentially mean 50+ forms on the page depending on how many users I have.
I'd appreciate some advice on this issue.
Thanks
A GET request should never change the state of your application, i.e. it shouldn't delete or modify users.
I'd use the ASP.Net Membership Provider and call its respective methods in the Controller Actions.

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.

To provide confirmation message in web page only if any change is done on the page

I want to show a confirmation message on click of cancel button and then redirect to another page. But the confirmation message should come only if any change has been done on the page.
Please can anyone help on this. Is it possible to achieve this at client side or at server side without comparing data ?
Thanks in advance.
I'm guessing you could have a scenario where:
A form is populated with values and each form item that you are tracking for changes also has a corresponding hidden field.
If the user clicks submit = great, but if the user clicks cancel you want to loop through each form item, comparing it to it's original value stored in the hidden field. If a change is detected you want to show a pop-up window informing the user that if they cancel their changes will lost - or something like that.
You can do all this on the client. JQuery, or Knockout (which I would use) or Backbone (I don't know this one tbh) should all provide a neat way to achieve this.
If you can't have hidden fields you might want to send the new values to the server in an Ajax post, get the server to pull the original values, compare and send back an appropriate response to the client. I'm sure there are other ways too*.
edit: like pop the original values in an array via Javascript, then compare them. Anyway, I'm sure you get the idea :)
This could be accomplished using jquery you would need to compare data since you want the message to come up when the cancel button is clicked.
jquery reference

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.

Suggest something for storing history

i have a ajaxified list page showing some list of items i am using a user control for filtering the content of the list page there are multiple filters in the user control what i want is if a user applied more than one filter than navigate to other page and press the back button the applied filters should be there in active state.
can any one please suggest me something how to achieve this.
You should put the filters in the query string because:
(1)It's not secret data.
(2)When user click "back" button of the browser, of course he wants the list is well-filtered, instead of that he need to filter the list again. Also sometimes he may copy the url in the address bar and sent it to others.
Format the filters' info in a proper way, e.g. "filter1=name&filter2=price", and deal with the query string in Page_Load if there is a filter.
One way is using cookies.
Store the values in a cookie, and read them back with javascript when the user hits the back button.
Or use the querystring like #Danny says. Why didn't I think of that right away :)

Categories