locked
Remembering Dropdown Selection Only During Validation RRS feed

  • Question

  • User-701766627 posted

    Hello,

    I have a form that combines dropdown and input fields. I am trying to keep a user's dropdown selection only when he gets a validation error and is prompted to fill in the required fields. After the form is submitted by the user, the dropdown field should be empty (ready for a new entry).

    Here is my code:

    @{   
        Validation.RequireField("Project", " **");
        Validation.RequireField("System", " **");
        Validation.RequireField("Operator", " **");
     
        var Project_ = Request.Form["Project"];
        var System_ = Request.Form["System"];
        var Operator_ = Request.Form["Operator"];
     
        var SearchTerm = "";
        var db = Database.Open("MissionSummary");
     
        if(IsPost)                                                         
        {
            if(Validation.IsValid())
            {
                var InsertCommand = "INSERT INTO Mission ([Project],[System],[Operator]) VALUES(@0,@1,@2)";
                db.Execute(InsertCommand,Project_,System_,Operator_);
     
                Project_ = "";
                Operator_ = "";
            }
            else
            {
                <script>
                    alert("Please fill in the indicated fields (**)");
                </script>
            }
        }
    }
     
    <form method="post">
        <table>
            <tr>
                <td><label>Project:</label></td>
                <td><input type="text" name="Project" style="width: 136px;" value="@Project_" id="Project"/>@Html.ValidationMessage("Project")</td>
            </tr>
            <tr>
                <td><label>System:</label></td>
                <td>
                    <select name="System" id="Dropdown">
                        <option value=""></option>
                        <option selected=@(Request["System"] == "US") value="US">US</option>                                                         
                        <option selected=@(Request["System"] == "FR") value="FR">FR</option>
                        <option selected=@(Request["System"] == "CA") value="CA">CA</option>
                    </select>@Html.ValidationMessage("System")
                </td>
            </tr>
            <tr>
                <td><label>Operator:</label></td>
                <td>
                    <select name="Operator" id="Dropdown">
                        <option value=""></option>
                        <option selected=@(Request["Operator"] == "ONE") value="ONE">ONE</option>
                        <option selected=@(Request["Operator"] == "TWO") value="TWO">TWO</option>
                        <option selected=@(Request["Operator"] == "THREE") value="THREE">THREE</option>
                        <option selected=@(Request["Operator"] == "FOUR") value="FOUR">FOUR</option>
                    </select>@Html.ValidationMessage("Operator")
                </td>
            </tr>
        </table>
        <input type="submit" value="Submit" id="SubmitButton"/>
    </form>

    The issue lies in the dropdown field "Operator". The dropdown field should be left empty after the form is submitted, like the field "Project".

    Thanks in advance for your help.

    Monday, November 27, 2017 5:11 PM

Answers

  • User475983607 posted

    Do a redirect

    if(Validation.IsValid())
    {
    	var InsertCommand = "INSERT INTO Mission ([Project],[System],[Operator]) VALUES(@0,@1,@2)";
    	db.Execute(InsertCommand,Project_,System_,Operator_);
    
    	Response.Redirect("~/thispage.aspx")
    }

    Or change the markup to use the Operator_  variable rather than the forms collection.

    <select name="Operator" id="Dropdown">
    	<option value=""></option>
    	<option selected=@(Operator_ == "ONE") value="ONE">ONE</option>
    <option selected=@(Operator_ == "TWO") value="TWO">TWO</option>
    <option selected=@(Operator_ == "THREE") value="THREE">THREE</option>
    <option selected=@(Operator_ == "FOUR") value="FOUR">FOUR</option>
    </select>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 27, 2017 6:07 PM