How to retain state in MVC - asp.net-mvc

I have an asp.net mvc5 web application which has 5 pages. The information on the last three pages are passed from the first two. So it’s like my first two pages are dynamic and last three changes according to the first two.
My program is only doing the following thing
I fill information in first page, hit submit button via post request, fill information in second page and hit submit button from second page. The rest pages display the result only.
But the problem arises when I press back button from browser form second page, change some value in first page and press submit button, I lose all the data from the second page. I want the second page retain its state whatever it was before.
I really don’t know how hard it is. So can some please advise me how to resolve that problem?
Thanks in advance.Here is just Sample code to illustrate This is the second page
<form action="thirdpage.cshtml" method="post">
MARKET VALUE OF HOUSE 2nd page:
<input id="MVOH" type="number" step="any" name="MVH" /><br/><br/>
MORTGAGE OWING 2nd page:
<input id="MO" type="number" step="any" name="MOwing" /><br/><br/>
MORTGAGE REPAYMENT 2nd page
<input id="MPPM" type="number" name="MP" />
<select id="Sel">
<option value="0">Per week</option>
<option value="1">Per fortnight</option>
<option value="2">Per Month</option>
</select><br/><br/>
<div>
<input id="next" type="submit" value="Income Expenses »"/>
</div>
</form>
And the first page is
<form action="secondpage.cshtml" method="post">
MARKET VALUE OF HOUSE:
<input id="MVOH" type="number" step="any" name="MVH" /><br/><br/>
MORTGAGE OWING:
<input id="MO" type="number" step="any" name="MOwing" /><br/><br/>
<td>MORTGAGE REPAYMENT</td>
<td>
<input id="MPPM" type="number" name="MP" />
<select id="Sel">
<option value="0">Per week</option>
<option value="1">Per fortnight</option>
<option value="2">Per Month</option>
</select><br/><br/>
<div>
<input id="next" type="submit" value="Income Expenses »"/>
</div>
</form>
Please I have provided only a part of code because my page is so big.

The only way to do this is to transfer the changes real time to the server, in the keypress/ change events. You could use an ajax call for this.
After the user navigates to this second page for the second time, you can fill the values he enters earlier

Firstly If you are dealing with ASP.NET MVC, I would strongly recommend you to use HTML Helpers to create your view like HTML.TextBox() or HTML.Label() etc, and bind these elements with your model so that at every request the values will be updated as per the type of Request GET / POST.
Secondly Pressing browser's back/forward button shows you the last cached page, due to which the values may be obsolete, I suggest you to have your own next previous button and initiate get request for the same.
In case you are troubled by the browser's button, you can check the page referrer accordingly and have that check at every post request

Related

Get Textbox Value in New Page in Sitecore MVC

I am using Sitecore 7.1. I created one search page and created a textbox by using the Web Form For marketer in it. On button click I want catch the text box value in a new page name search-result. How can I catch the WFFFM text box value in New page? I Don't want use JavaScript click event and query string.
<form class="search-form" action="/searchresults" method='get' >
<label><input type="text" name="searchText" id="searchText" placeholder="#placeholderText"></label>
<input type="submit" name="submit" value="" class="#cssClass">
</form>
But i am getting below type of URL
http://www.xyz/searchresults?searchText=searchTextHere&submit=
Can you capture the "onsubmit" event and update the URL to whatever you need using Javascript? You only said not to use onclick event. I'm assuming that you may want to have /searchresults/ as a result. Of course, I assume that your routes are set for this and searchresults can process this correctly.

Paypal select amount for Hosted Donation button in rails 4

I am working on rails 4 application. I need to give payment options for Donation button.
I need to create radiobutton as well as textbox to enter amount of payment for Donation.
My code is
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="xxxxxxxxxxx">
<input type="hidden" name="amount" value="">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
Here I enter value of amount field by jQuery (code is not posted) so it is not empty ("").
But on the second step the amount I submit in hidden field is not shown.
I google it some people say one can not enter amount on hosted button means it would be fixed every time.
Some people say create the buy now button reference => https://www.paypal.com/webapps/mpp/get-started/donate-button-text-field
What should I do is it possible with hosted button?
Should I change my code ?
Whenever you use the PayPal hosted buttons you can't change the amount related information. Hosted buttons are secure in the terms that nobody can fiddle with the amount. Changes to the amount for hosted buttons can only be done thru account or via API's . If you are implementing a solution where you need to change the amount dynamically you should consider using the clear text button or You can dynamically generate the encrypted using the API's
However the variables related to :address ,cancel/return/image/ipn url ,item name/invoice id/custom/quantity can be overridden by passing the variables for hosted buttons.

Rails will_paginate custom renderer manual page number

Hy
What i want to do is to create a custom renderer for will_paginate which renders first, previous, next and last page and a input field where the user can type in the page number manually. I already have the links for first, last etc. but i stuck at the input field. I could create a form in the view but the input field has to be rendered between the previous and next links.
Does anyone know how to do this?
Thanks for your help
You can do this as a separate form (make sure it is a GET). All you
need is the one input element named page. Something as simple as this
should work (not all browsers may like the #). I dropped it into a
site I'm playing with now and it worked. Put it anywhere on your page.
You might have to make something more complicated if you need to
incorporate search terms.
<form action="#" method="get">
Go to page: <input type="text" name="page" value="" size="2"
maxlength="4" />
<input type="submit" name="btnSubmit" />
</form>

MVC - Input submit causing redirect automatically

I have the following form
<form id="file_upload" action="/Upload/Save" method="POST" enctype="multipart/form-data">
<input type="text" id="txtProposalName" name="name" placeholder="Nome da Camiseta" />
<input type="text" id="txtProposalDesc" name="description" placeholder="Descrição da Camiseta"/>
<div class="fileupload-buttonbar">
<div class="progressbar fileupload-progressbar"></div>
<span class="fileinput-button">
Upload images
<input type="file" name="files[]" multiple />
</span>
</div>
<input type="hidden" id="imgID" name="imgID"/>
<input type="submit" id="postProposal"/>
Would call this action:
[HttpPost]
public JsonResult Save(string name, string description, string imgID)
{
return Json("a");
}
(This is the current implementation, it has no logic because I am still testing some things).
My problem is: when I click on my submit button, the action is called with the correct values, but when it returns, my browser redirects to /Upload/Save (which is the action URL).
Why is this happening? Is there any way to prevent it?
Thanks!
You can use an asynchronous call to your method (e.g. AJAX) to prevent the page from reloading.
You can use Ajax.BeginForm to prevent reload page.
As I can see you use full page reload with:
action="/Upload/Save"
Also you trying to post data to json action method in controller. You need all form submit make with jquery, most problems you will find with ajax file uploaders.
Added:
Also look at serialize() it will help you to collect all form input values.
Dont need to prevent it, just redirect the user in your server code .

asp.net mvc post to different views in same form

I have form area in my view. If I click button A, I want to submit to /Books/1 and if I click button B, I want to submit to /Books/2
How do I achieve this with MVC?
<form id="form1" name="form1" action="/Books/" method="get">
<input type="text" name="search" value="">
<input type="submit" name="id" value="1">
<input type="submit" name="id" value="2">
</form>
It sounds like what you want to do is call the Books Controller, with, say, the Search action. So for instance you might want to call /Books/Search/<search expression>/1, or /Books/Search/<search expression>/2, etc. (There's a few different ways you could be formatting these URLs, but it's mostly a matter of personal preference I think) If you want the URLs to appear as you've got them above (without the action in the URL), that can be accomplished with routing, something like this:
routes.MapRoute(
"Books",
"Books/{searchExpr}/{pageId}",
new { controller = "Books", action = "Search", searchExpr = "", pageId = 1 }
);
I think the main problem is that you're trying to use the WebForms PostBack everything paradigm in a situation where you're probably better off sending the information to the server in the URL or query string. The only time you're actually going to be posting form data here is when the user actually types something into the search box and clicks the Search button - at that point, the controller will pass the search expression to the appropriate View by stuffing it in ViewData, and from there, the View can pull it out and repopulate that textbox on the results page.
MVC Views can have multiple forms on a 'page', so just create separate sections and give each one their own form action.
<form id="form1" name="form1" action="/Books/1" method="get">
<!--...form fields-->
</form>
<form id="form2" name="form2" action="/Books/2" method="get">
<!--...form fields-->
</form>
I have never seen the ability to have a form field attached to two forms, seems like it wouldn't work. What you can do is put a hidden field in the second form which, on submission, grabs the information from the textbox in the first form.

Resources