Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:55:48+00:00 2026-06-03T06:55:48+00:00

i’ve two table in DB. i want to create dropdownlist for city and corresponding

  • 0

i’ve two table in DB.
i've two table city,area

i want to create dropdownlist for city and corresponding area after that.this is my first cascade drodown menu in my entire life.i’ve tried to follow some examples.in the get action i’ve :

      ViewBag.cities = db.cities.ToList();
      ViewBag.areas = db.areas.ToList();

in the view i’ve:

       <div class="editor-field">
         @Html.DropDownListFor(model => model.city,new SelectList(ViewBag.cities as 
         System.Collections.IEnumerable, "city_id", "name"),
         "Select City", new { id = "ddlCars" }) 
         @Html.ValidationMessageFor(model => model.city)
       </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.area)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.area, new 
         SelectList(Enumerable.Empty<SelectListItem>(), "area_id", "name"),
         "Select Area", new { id = "ddlModels" })
        @Html.ValidationMessageFor(model => model.area)
    </div>

i’ve just copied a js file from a site that is

       $(document).ready(function () {
       $("#ddlCars").change(function () {
       var idModel = $(this).val();
       $.getJSON("/Post/LoadAreasByCity", { id: idModel },
       function (carData) {
        var select = $("#ddlModels");
        select.empty();
        select.append($('<option/>', {
        value: 0,
       text: "Select Area"
       }));
       $.each(carData, function (index, itemData) {

        select.append($('<option/>', {
        value: itemData.Value,
        text: itemData.Text
         }));
        });
        });
        });


       $("#ddlModels").change(function () {
       var idColour = $(this).val();
      $.getJSON("/Home/LoadColoursByModel", { id: idColour },
      function (modelData) {
      var select = $("#ddlColours");
      select.empty();
      select.append($('<option/>', {
      value: 0,
      text: "Select a Colour"
      }));
     $.each(modelData, function (index, itemData) {

      select.append($('<option/>', {
      value: itemData.Value,
      text: itemData.Text
      }));
      });
      });
      });
      });

in my Post/LoadAreasByCity my method is:

          public JsonResult LoadAreasByCity(string id)
          {
           PostManager pm = new PostManager();
           if ( id=="") { id = "1"; }
           int c_id =(int) Convert.ToInt32(id);
           var areaList = pm.GetModels(c_id);

           var modelData = areaList.Select(m => new SelectListItem()
               {
              Text = m.name,
              Value = m.area_id.ToString(),

           });
           return Json(modelData, JsonRequestBehavior.AllowGet);
           } 

it propagate cities and area is correctly in the view page.but after submitting the data it gives errorin this line

         @Html.DropDownListFor(model => model.city,new SelectList(ViewBag.cities as   
         System.Collections.IEnumerable, "city_id", "name"),"Select City", new { id = 
         "ddlCars" }) 

it says Value cannot be null.
Parameter name: items

Finally in my post action

                int c_id = Convert.ToInt32(p.city);
                int a_id = Convert.ToInt32(p.area);
                area a = db.areas.Single(x=>x.area_id == a_id);
                city c = db.cities.Single(s => s.city_id == c_id);
                post.city = c.name;
                post.area = a.name;
                ....Save to DB

what is the problem in this ….. thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:55:49+00:00Added an answer on June 3, 2026 at 6:55 am

    I suspect that in your POST action you have redisplayed the same view but forgot to populate the ViewBag.cities and ViewBag.areas items as you did in your GET action:

    [HttpPost]
    Public ActionResult Process()
    {
        ... Save to DB
    
        ViewBag.cities = db.cities.ToList();
        ViewBag.areas = db.areas.ToList();   
        return View();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.