Click here to Skip to main content
14,865,287 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey there i'm new on this

i already create cascading dropdownlist by script jason

but when i try to insert this data i can only insert Country but States & City not insert why States & City not insert to database only just Country and here my code

HTML
<!--Country-->>
        <div class="form-group">
            @Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("countryId", (SelectList)ViewBag.SelectCountry, "select please", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Country, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <!--States-->>
        <div class="form-group">
            @Html.LabelFor(model => model.States, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <select id="statelistdrop" class="form-control" name=""></select>
                @Html.ValidationMessageFor(model => model.States, "", new { @class = "text-danger" })
            </div>
        </div>
 
        <!--City-->>
        <div class="form-group">
            @Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <select id="citieslistdrop" class="form-control"></select>
                @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
            </div>
        </div>


and here my script for cascading dropdownlist

JavaScript
<script>
    $(function () {
        $("#countryId").change(function () {
            $.get("/Country/GetStatesById", { ID: $("#countryId").val()}, function (data) {
                $("#statelistdrop").empty();
                $.each(data, function (index,row) {
                    $("#statelistdrop").append(" <option value='" + row.state_id + "'>" + row.name+ "</option>")
                });
            })
        });
    });
 
</script>
 
<script>
    $(function () {
        $("#statelistdrop").change(function () {
            $.get("/Country/GetCitiesById", { ID: $("#statelistdrop").val() }, function (data) {
                $("#citieslistdrop").empty();
                $.each(data, function (index,row) {
                    $("#citieslistdrop").append(" <option value='" + row.state_id + "'>" + row.name + "</option>")
                });
            })
        });
    });
 
</script>


What I have tried:

i try to change razor code but didn't work
Posted
Updated 14-Nov-16 6:52am
Comments
Suvendu Shekhar Giri 14-Nov-16 1:17am
   
Where is the code for INSERT?
ナイト ロイヤル 14-Nov-16 2:58am
   
[HttpGet]
public ActionResult Create()
{
List<category> catlist = db.Categories.ToList();
SelectList sl = new SelectList(catlist.AsEnumerable(), "id", "name");
ViewBag.SelectCategories = sl;

List<country> countrylist = db.CountryTb.ToList();
SelectList s2 = new SelectList(countrylist.AsEnumerable(), "id", "name");
ViewBag.SelectCountry = s2;

return View();

}

[HttpPost]
public ActionResult Create(Item i)
{
db.Items.Add(i);
db.SaveChanges();
return RedirectToAction("Index");

}

1 solution

Quote:

<select id="statelistdrop" class="form-control" name=""></select>
<select id="citieslistdrop" class="form-control"></select>

In order to send the selected value back to the server when the form is posted, both of these controls need a name.

In order for the model binder to map the posted data to the properties on your model, that name needs to match the name of the corresponding property on your model.

Assuming the properties are named sensibly:
HTML
<select id="statelistdrop" class="form-control" name="stateId"></select>

<select id="citieslistdrop" class="form-control" name="cityId"></select>
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900