locked
Preserving form after Validation Check RRS feed

  • Question

  • User1771308999 posted

    This might be a repeat of an older post, I'm trying to find out how to maintain values selected on Form that has been filled out, but after Validation check and when that fails, the page reload and all the values entered are empty.

    Examples, a form that has first name, last name, and radio selection of favorite movies.

    The user enter first name and last name but did not select any of the radio selection.

    The validation check and found that the radio select was not selected, reload the page, display validation summary.

    The user that entered the first name and last name are gone and is forced to retype them. I'd like to prevent that.

    Tuesday, August 13, 2013 4:56 PM

Answers

  • User-1980594115 posted
    @Request.QueryString["searchGenre"]

     is one means of passing URL data NOT Form data.

    @Request.QueryForm["searchGenre"]

    is for passing Form data.

    @Request["searchGenre"]

    is for passing either URL or Form data.

    Here is an example of using a boolean (true/false) checkbox and preserving the value:

    @{
        bool test = false;
    
        if (IsPost){
    	// Convert String Form Variable To Boolean Value
            test = Request.Form["insurance"].AsBool();
    		
    	// View String Form Variable And Boolean Variable Values
            @: Insurance: @Request.Form["insurance"];
            @: Insurance Test:  @test <br />
        }
    }
    
    <form method="post">
    Has Insurance?: <input type="checkbox" name="insurance" value="true" @(test ? "checked=\"checked\"" : "") />
    <input type="submit" />
    </form>
    
    

    Or here is an example using Request.Form variable as the value in the form field. Note the AsBool() in the form input field to convert the returned form variable into boolean true/false:

    @{
        if (IsPost){
    	// View String Form Variable
            @: Insurance: @Request.Form["insurance"];
        }
    }
    
    <form method="post">
    Has Insurance?: <input type="checkbox" name="insurance" value="true" @(Request.Form["insurance"].AsBool() ? "checked=\"checked\"" : "") />
    <input type="submit" />
    </form>
    






    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 19, 2013 11:29 AM
  • User-1980594115 posted

    Same principle for radio button. The previous example should work for boolean expressions. Though I usually prefer to use different values for radio selections than true/false, such as the following:

    @{
    
    	if (IsPost) {
    		// Display Results of Match Selection
    		@:Match Value: @Request.Form["match"] <br />
    	}
    }
    
    <form method="post" id="TheForm">
    	Unmatched Only:
    	&nbsp;&nbsp;
    	<input type="radio" name="match" 
    		id="match1"
    		tabindex="100" 
    		value="1" @(Request.Form["match"] == "1" ? " checked=\"checked\"" : "") />
    	&nbsp;&nbsp;
    
    	Matched Only:
    	<input type="radio" name="match" 
    		id="match2"
    		tabindex="110" 
    		value="2" @(Request.Form["match"] == "2" ? " checked=\"checked\"" : "") />
    	&nbsp;&nbsp;
    
    	ALL:
    	<input type="radio" name="match" 
    		id="match3"
    		tabindex="120" 
    		value="3" @(Request.Form["match"] == "3" ? " checked=\"checked\"" : "") />
    
        <input type="submit" />
    </form>
    

    To preserve the value in textarea after Post, see the following example:

    @{
    
    	if (IsPost) {
    		// Display Results of Match Selection
    		@:Description Value: @Request.Form["Description"] <br />
    	}
    }
    
    <form method="post" id="TheForm">
    
        <textarea name="Description" 
    		cols="28" rows="3" 
    		id="Description" tabindex="110"
    		placeholder="Enter Description">@Request.Form["Description"]</textarea>
        <br />
    
        <input type="submit" name="submit" value="submit" />
    </form>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 20, 2013 11:41 AM

All replies

  • User-1496281956 posted

    set value attribute for html controls. 

    When you request html controls make sure you do it like this:

    var firstname = "";
    var lastname = "";
    
    
    if (IsPost)
    {
    
    firstname = Request["firstname"];
    lastname = Request["lastname"];
    
    }

    if you create variables in the if statement they will become local and you cannot use it outside so you would not be able to use it in the value attribute when they are declared outside first they are global. Set it like this:

    <input type="text" value="@firstname"/>



    Tuesday, August 13, 2013 5:08 PM
  • User-821857111 posted

    Or simply set the value to Request["firstname"]

    <input type="text"name="firstname" value="@Request["firstname"]" />

    Tuesday, August 13, 2013 5:14 PM
  • User1771308999 posted

    What about using radio input, checkbox input, etc??

    Wednesday, August 14, 2013 12:57 PM
  • User-821857111 posted

    That's slightly different. In both of those cases, you need to set checked="checked". You cna so this using Conditional attributes: http://www.mikesdotnetting.com/Article/201/Cleaner-Conditional-HTML-Attributes-In-Razor-Web-Pages

    Wednesday, August 14, 2013 1:50 PM
  • User1771308999 posted

    Looking through the tutorial, I found that this code: 

    <mark>@Request.QueryString["searchGenre"]</mark>

    Is what is used to "remember" user's input.

    However, the fields are within a form. So once I submit the form (using post) and Validation check did it job and find that a field didn't meet it's criteria (using RequiredField.Validation) all of the fields in the form returns empty.

    So no luck so far..

    <mark></mark>
    Thursday, September 19, 2013 11:20 AM
  • User-1980594115 posted
    @Request.QueryString["searchGenre"]

     is one means of passing URL data NOT Form data.

    @Request.QueryForm["searchGenre"]

    is for passing Form data.

    @Request["searchGenre"]

    is for passing either URL or Form data.

    Here is an example of using a boolean (true/false) checkbox and preserving the value:

    @{
        bool test = false;
    
        if (IsPost){
    	// Convert String Form Variable To Boolean Value
            test = Request.Form["insurance"].AsBool();
    		
    	// View String Form Variable And Boolean Variable Values
            @: Insurance: @Request.Form["insurance"];
            @: Insurance Test:  @test <br />
        }
    }
    
    <form method="post">
    Has Insurance?: <input type="checkbox" name="insurance" value="true" @(test ? "checked=\"checked\"" : "") />
    <input type="submit" />
    </form>
    
    

    Or here is an example using Request.Form variable as the value in the form field. Note the AsBool() in the form input field to convert the returned form variable into boolean true/false:

    @{
        if (IsPost){
    	// View String Form Variable
            @: Insurance: @Request.Form["insurance"];
        }
    }
    
    <form method="post">
    Has Insurance?: <input type="checkbox" name="insurance" value="true" @(Request.Form["insurance"].AsBool() ? "checked=\"checked\"" : "") />
    <input type="submit" />
    </form>
    






    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 19, 2013 11:29 AM
  • User-821857111 posted

    Looking through the tutorial, I found that this code: 
    @Request.QueryString["searchGenre"]

    Is what is used to "remember" user's input.

    However, the fields are within a form. So once I submit the form (using post) and Validation check did it job and find that a field didn't meet it's criteria (using RequiredField.Validation) all of the fields in the form returns empty.

    So no luck so far..

    So why don't you follow the suggestions provided in this thread already and use the shortened form to refer to the Request object: Request["searchGenre"]? Doing so means that all the Request collections are searched for items with the specified key: QueryString, Form, Cookies, etc. Then you don;t have to worry about whether the form was posted or submitted via GET (the default).

    Friday, September 20, 2013 12:45 AM
  • User1771308999 posted

    Thank you!

    @Request.Form["searchGenre"]

    Did the trick!

    Next question -- the sample you used for checkbox, not sure if you can apply this to radio button? Additionally, is it possible to preserve value in textarea after POST failed validation?

    Friday, September 20, 2013 10:46 AM
  • User-1980594115 posted

    Same principle for radio button. The previous example should work for boolean expressions. Though I usually prefer to use different values for radio selections than true/false, such as the following:

    @{
    
    	if (IsPost) {
    		// Display Results of Match Selection
    		@:Match Value: @Request.Form["match"] <br />
    	}
    }
    
    <form method="post" id="TheForm">
    	Unmatched Only:
    	&nbsp;&nbsp;
    	<input type="radio" name="match" 
    		id="match1"
    		tabindex="100" 
    		value="1" @(Request.Form["match"] == "1" ? " checked=\"checked\"" : "") />
    	&nbsp;&nbsp;
    
    	Matched Only:
    	<input type="radio" name="match" 
    		id="match2"
    		tabindex="110" 
    		value="2" @(Request.Form["match"] == "2" ? " checked=\"checked\"" : "") />
    	&nbsp;&nbsp;
    
    	ALL:
    	<input type="radio" name="match" 
    		id="match3"
    		tabindex="120" 
    		value="3" @(Request.Form["match"] == "3" ? " checked=\"checked\"" : "") />
    
        <input type="submit" />
    </form>
    

    To preserve the value in textarea after Post, see the following example:

    @{
    
    	if (IsPost) {
    		// Display Results of Match Selection
    		@:Description Value: @Request.Form["Description"] <br />
    	}
    }
    
    <form method="post" id="TheForm">
    
        <textarea name="Description" 
    		cols="28" rows="3" 
    		id="Description" tabindex="110"
    		placeholder="Enter Description">@Request.Form["Description"]</textarea>
        <br />
    
        <input type="submit" name="submit" value="submit" />
    </form>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 20, 2013 11:41 AM
  • User1771308999 posted

    Sorry for the late reply-

    Both works like a charm. Thank you very much, dblaire!!

    Wednesday, September 25, 2013 11:01 AM