locked
Populate a DropDownList control on an Update form RRS feed

  • Question

  • User1097139691 posted

    I have a form that contains a few textboxes and a drop down control where I use an Html.DropDownList helper. I have no problem adding a record to the database. However, I could use some assistance when it comes to using that same html DropDownList helper on an update form in a single database table. What is the code required to populate the value of that field into the update form?

    Here is the code pertaining to the Update page:

    @{
    var id = UrlData[0];
    if (id.IsEmpty()){
    Response.Redirect(@Href("~/todo/ToDoList"));
    }

    var task = "";
    var duedate = "";
    var completed = "";
    var owner = "";

    var db = Database.Open("ToDo");

    if(IsPost){
    task = Request.Form["task"];
    duedate = Request.Form["duedate"];
    completed = Request.Form["completed"];
    owner = Request.Form["owner"];

    var sqlCommand = "UPDATE todolist SET task = @0, duedate = @1, completed = @2, owner = @3 WHERE id = @4";
    db.Execute(sqlCommand, task, duedate, completed, owner, id);
    Response.Redirect(@Href("~/todo/ToDoList"));
    }

    var sqlSELECT = "SELECT * FROM todolist WHERE id=@0";
    var row = db.QuerySingle(sqlSELECT, id);

    task = row.task;
    duedate = row.duedate.ToShortDateString();
    completed = row.completed;
    owner = row.owner;
    }


    <h1>Edit Tasks</h1>
    <form action="" method="post">
    <fieldset>
    <legend>Tasks</legend>
    <div>
    @Html.Label("Task:", "task")
    @Html.TextBox("task", task)
    </div>
    <div>
    @Html.Label("DueDate:", "duedate")
    @Html.TextBox("duedate", duedate)
    </div>
    <div>
    @Html.Label("Completed:", "completed")
    @{
    var completedList = new List<SelectListItem>
    {
    new SelectListItem {Value = "Yes", Text = "Yes"},
    new SelectListItem {Value = "No", Text = "No"}
    };
    }
    @Html.DropDownList("completed", completedList, *What else do I place here?*)
    </div>
    <div>
    @Html.Label("Owner:", "owner")
    @Html.TextBox("owner", owner)
    </div>
    </fieldset>
    <div><input type="submit" value="Edit Task" />
    <input type="button" onclick="history.back()" value="Cancel" />
    </div>
    </form>

    Wednesday, August 19, 2015 5:16 PM

Answers

  • User1097139691 posted

    Thanks for responding.

    I just figured out what I needed to do to get everything to work properly:

    In the example that I gave, all I had to do was add the Selected  =  true to the SelectListItem. Once that was done, the DropDownList field on the update form showed the correct value. It's a filtered table with only uncompleted tasks, so that field would always be No until the task was completed.

    @Html.Label("Completed:", "completed")
    @{
    var completedList = new List<SelectListItem>
    {
    new SelectListItem {Value = "Yes", Text = "Yes"},
    new SelectListItem {Value = "No", Text = "No", Selected = true}
    };
    }
    @Html.DropDownList("completed", completedList, "No")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 19, 2015 8:06 PM

All replies

  • User2024324573 posted
     @Html.DropDownList("Customer", new SelectList(Model.Customer, "Id", "CustomerName"), "", new {@class = "form-control" })

    If you look at the different overloads of the helper, you can use @Html.DropDownList with a lot of options.

    For example, I have a dropdown list consisting a list of customers- having Id and CustomerName which are being from the controller action. To display in the dropdpown list you can use the above syntax, also you can add html attributes to the controls.

    Hope this will help.

    Wednesday, August 19, 2015 5:32 PM
  • User1097139691 posted

    Thanks for responding.

    I just figured out what I needed to do to get everything to work properly:

    In the example that I gave, all I had to do was add the Selected  =  true to the SelectListItem. Once that was done, the DropDownList field on the update form showed the correct value. It's a filtered table with only uncompleted tasks, so that field would always be No until the task was completed.

    @Html.Label("Completed:", "completed")
    @{
    var completedList = new List<SelectListItem>
    {
    new SelectListItem {Value = "Yes", Text = "Yes"},
    new SelectListItem {Value = "No", Text = "No", Selected = true}
    };
    }
    @Html.DropDownList("completed", completedList, "No")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 19, 2015 8:06 PM