-1

I have a drop down list populated with Ajax call, now i have to select populated value and store it in database table in controller action method. I go through these two answers on stack overflow link1, link2 and try provided solution but it not works.

Action method:

public ActionResult ClassCreated(Class c)
{
  c.Course.title = Request.[""];
  c.ClassName=Request["ClassName"];
  c.strength = Int16.Parse(Request["strength"]);
  c.rollNoPattern = Request["rollNoPattern"];
  db.Classes.Add(c);
  db.SaveChanges();
  return RedirectToAction("Index");
}

Jquery:

<script>
  $(document).ready(function () {
    $('#b1').click(function () {
      var userName = "Hello"
      $.getJSON("/classes/getCourseList?username=" + userName, function (data1) {
      var myOptions =
      {
        val1: data1.title
      };
      var $mySelect = $('#s1');
      $.each(myOptions, function (val, text) {
        $mySelect.append($('<option />',
        {
          value: val,
          text: text
        }));
      });
    });
   });
  });
</script>
Community
  • 1
  • 1
NN796
  • 1,247
  • 11
  • 31
  • what do you mean not works? – tech-gayan Mar 17 '15 at 10:45
  • All you have show is how you populate the dropdown (irrelevant to the question). Whats important is the name of the control and does it match the name of a property in your model –  Mar 17 '15 at 10:48
  • And why on earth are you doing `c.ClassName=Request["ClassName"];` etc. Your model `c` is already populated with those values. –  Mar 17 '15 at 10:59

1 Answers1

0
$("#b1").on("click", function () {

            var url = "/classes/getCourseList/";
            $.ajax({
                type: 'POST',
                url: url,
                dataType: "json",
                traditional: true,
                success: function (data) {

                    $("#dropdownId").empty();

                    $("#dropdownId").append($("<option>").val("").text("Please select Category"));
                    $.each(data, function () {
                        $("#dropdownId").append($("<option>").val(this.Value).text(this.Text));
                    });
                }
            });
            return false;
        });
Shahzad Khan
  • 432
  • 2
  • 14