1

I have this:

@Html.DropDownList("centres", (List<SelectListItem>)ViewData["centres"])

And I need to update it with data entered into the database with SQL while the program is running, using JQuery:

jQuery(function ($) {
    jQuery("body").on('click', "#create", function () {
        $.getJSON("/Centres/updateList", function (results1) {
// perform update here using results1 from jsonresult updateList in Controller class
        });
    });
});

The results1 is a List from my MVC controller class. I'd like to be able to edit my existing html dropdownlist with these new results. Currently it displays a list of selectListItems, but in updateList in the controller class I am creating a new list, based on a new centre I have just created. My Controller class method looks like this:

public JsonResult updateList()
{
    List<SelectListItem> centres = new List<SelectListItem>();
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Michael\Documents\Visual Studio 2012\Projects\OneEightyWebApp\OneEightyWebApp\App_Data\OneHoldings.mdf;Integrated Security=True");
    SqlCommand cmd = new SqlCommand("SELECT Centre.Centre_Name, Count(Shop_No) AS Shop_Count FROM Centre LEFT JOIN Space ON Centre.Centre_Name = Space.Centre_Name GROUP BY Centre.Centre_Name;", conn);
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    conn.Open();
    da.Fill(dt);
    conn.Close();
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        centres.Add(new SelectListItem { Text = dt.Rows[i].ItemArray.GetValue(0).ToString(), Value = dt.Rows[i].ItemArray.GetValue(1).ToString() });
    }
    return Json(centres, JsonRequestBehavior.AllowGet);
}

Any ideas?

3
  • Should we guess what you get from the server? Commented Mar 27, 2013 at 22:33
  • It's a list. a list of centre names, created in updateList. I create more centres then need to update this <select> with the new centres. Commented Mar 27, 2013 at 22:34
  • Please improve your question, post the JSON response/outputted html and specify how the select element should be updated. Commented Mar 27, 2013 at 22:36

2 Answers 2

2

You can rebuild the drop-down list using jquery

$.getJSON("/Centres/updateList", function (results1) {
   //Clear out the old values
   $("#centres").empty();
   //Add the input items back in
   $.each(results1, function (key, val) {
      $("#centres").append($("<option></option>").attr("value", val.Value).text(val.Text));
   });
});

One of the advantages of using MVC is the html it produces is quite simple and can be easily reproduced with simple jQuery commands.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the attr function repeatedly, so you can pre-set selections by

if(val.Selected) {
  $("#centres").append($("<option></option>")
      .text(val.Text)
      .attr("value", val.Value)
      .attr("selected", "selected"));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.