Search code examples
razorwebmatrixasp.net-webpages

Remember selection from Dropdown menu in Webmatrix


I am trying to keep the value selected by the user in the following Dropdown menu:

<form method="post">
<p><label for="ProjectManager">Project Manager:</label>
    <select name="ProjectManager">
        <option value="" selected>Select...</option>
        <option selected=@(Request["ProjectManager"] == "One") value="One">One</option>
        <option selected=@(Request["ProjectManager"] == "Two") value="Two">Two</option>
        <option selected=@(Request["ProjectManager"] == "Three") value="Three">Three</option>
        <option selected=@(Request["ProjectManager"] == "Four") value="Four">Four</option>
        <option selected=@(Request["ProjectManager"] == "Five") value="Five">Five</option>
        <option selected=@(Request["ProjectManager"] == "Six") value="Six">Six</option>
    </select>
</p>
<p><input type="submit" name="ButtonConfirm" value="Confirm" /></p>
</form>

Just for your reference, here is what happens to the database in the background:

@{
    var ProjectManager = "";
    if(IsPost){
        ProjectManager = Request.Form["ProjectManager"];
        db = Database.Open("ControlPoints");
        var InsertCommand = "INSERT INTO ControlPointName ([Project_Manager]) VALUES(@0)";
        db.Execute(InsertCommand, ProjectManager);
    }
}

My code works fine, it just won't remember the value selected. As you can see, I have been trying to implement selected=@(Request["ProjectManager"] == "One") with no luck so far. What am I doing wrong here?

Thanks in advance.

UPDATE:

In the following link http://forums.asp.net/t/1899268.aspx?Remembering+Dropdown+Box+selection+in+WebMatrix a very similar issue is solved. The difference is that the form method is 'get' rather than 'post' as in my case.


Solution

  • I figured it out. It is the strangest thing, in one of my comments I had the following <!--The user's input.....--> After I removed the ' from the word user's, everything worked as needed.