Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind Mvc Model using with html <select> tag

i have one drop down list, this list bind value using ajax call

<script type="text/javascript">
function getCityName(data) {
    $('#ddlCity').empty();
    if (data != 0) {
        $.ajax({
            type: "POST",
            url: "/Home/GetCityList",
            data: { 'CityId': data },
            cache: false,
            //contentType: "application/json",
            datatype: "JSON",
            success: function (result) {
                var cityData = result.Data;
                var defaultV = new Option("--Select--", 0, true);
                $('#ddlCity').append(defaultV);
                for (var i = 0; i < cityData.length; i++) {
                    var opt = new Option(cityData[i].CityName, cityData[i].Id);
                    $('#ddlCity').append(opt);
                }
            }
        });
    }
    else {
        alert('Select State');
    }
}

<div>@Html.LabelFor(c => c.CityId)</div>
<div><select id="ddlCity"></select></div>

hear i want to bind model prop CityId in model class how to bind it...

like image 858
Naimish Mungara Avatar asked Jun 09 '26 09:06

Naimish Mungara


2 Answers

element needs a name attribute to match the model property. add CityId in name attribute of select tag:

<div><select id="ddlCity" name = "CityId"></select></div>
like image 128
Jaimin Dave Avatar answered Jun 11 '26 23:06

Jaimin Dave


the <select> tag need MVC Name property to add, @Html.NameFor(x=>x.Entity Property) and name property bind value in model. html code below :

<div><select id="ddlCity" name="@Html.NameFor(c=>c.CityId)"></select></div>

thanks for responding to answer this topic.

like image 43
Naimish Mungara Avatar answered Jun 11 '26 22:06

Naimish Mungara