[Solved] DropDownListFor selected item not save in database in ASP.Net MVC

leila398
 
on Nov 25, 2021 01:52 AM
259 Views

I have 2 dropdownlists. The first contains names, the 2nd contains data pulled in from the database via a javascript function. This works fine.

All data from the form is saved except the 2nd dropdownlist. This dropdownlistfor is initially set to an empty list when the form is rendered since it depends on the selected value of another dropdownlist. Here is my 2nd dropdownlist that is not saving to the db: 

@Html.DropDownListFor(model => model.file_ref, Enumerable.Empty<SelectListItem>(), "Select", new { @class = "form-control" })

Please can you advise what is wrong with this and why its value won't save to the DB?

Download FREE API for Word, Excel and PDF in ASP.Net: Download
leila398
 
on Nov 25, 2021 05:56 AM

Solution was an error in Javascript function below. Instead this.Value, I had this.Val which meant the value was not passing through.

success: function (Matter) {
    $.each(Matter, function () {
        $("#ddl").append($("<option />").val(this.value).text(this.text));            
    });
},
error: function () {
    alert("Error finding data");
}