2016-11-16 15 views
-1

我有這個表名稱項目,當我編輯表格的一行時,它會爲狀態和城市下拉列表返回空值。但在數據庫中已添加,但下拉列表顯示空,我不知道如何檢索選定值 ,在這裏,我是用當我單擊編輯按鈕時,MVC從數據庫檢索選定值到級聯onload

這裏表項目項目控制器

[Table("Item")] 
public class Item 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int id { get; set; } 
    [Required] 
    public string name { get; set; } 
    public string info { get; set; } 
    public decimal price { get; set; } 

    public int CatId { get; set; } 

    public int? CountryId { get; set; } 

    public int? StateId { get; set; } 

    public int? CityId { get; set; } 

    [ForeignKey("CatId")] 
    public virtual Category Category { get; set; } 

    [ForeignKey("CountryId")] 
    public virtual Country Country { get; set; } 

    [ForeignKey("StateId")] 
    public virtual States States { get; set; } 

    [ForeignKey("CityId")] 
    public virtual City City { get; set; } 

} 

代碼編輯我所有的代碼

[HttpGet] 
    public ActionResult Edite(int id) 
    { 

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


     Item I = db.Items.Single(x => x.id == id); 

     return View(I); 
    } 


    [HttpPost] 
    [ActionName("Edite")] 
    public ActionResult Edite_post(int id) 
    { 
     Item I = db.Items.Single(x => x.id == id); 
     UpdateModel(I, null, null, new string[] { "id" }); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

,這是我的jQuery代碼,但我認爲這是唯一的工作電平變化我想onload事件也它的工作在檢索選定值

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

</script> 

<script> 
    $(function() { 
     $("#StateId").change(function() { 
      $.get("/Country/GetCitiesById", { ID: $("#StateId").val() }, function (data) { 
       $("#CityId").empty(); 
       $.each(data, function (index, row) { 
        $("#CityId").append(" <option value='" + row.id + "'>" + row.name + "</option>") 
       }); 
      }) 
     }); 
    }); 

</script> 

這裏我的HTML代碼

<!--Country--> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.CountryId, 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.CountryId, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <!--States--> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.StateId, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("StateId", new SelectList(Enumerable.Empty<SelectListItem>(), "state_id", "name"), "-- Select --", new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.StateId, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <!--City--> 
     <div class="form-group"> 
      @Html.LabelFor(model => model.CityId, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("CityId", new SelectList(Enumerable.Empty<SelectListItem>(), "id", "name"), "-- Select --", new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.CityId, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

這裏表類別

[Table("Category")] 
public class Category 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int id { get; set; } 

    [Required] 
    public string name { get; set; } 

    public virtual ICollection<Item> Items { get; set; } 
} 

這裏表國家

[Table("countries")] 
public class Country 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int id { get; set; } 

    public string sortname { get; set; } 

    public string name { get; set; } 

    public virtual ICollection<States> states { get; set; } 

    public virtual ICollection<Item> Items { get; set; } 
} 

這裏表美國

[Table("states")] 
public class States 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int state_id { get; set; } 

    public string name { get; set; } 

    public int country_id { get; set; } 

    [ForeignKey("country_id")] 
    public virtual Country countries { get; set; } 

    public virtual ICollection<City> cities { get; set; } 

    public virtual ICollection<Item> Items { get; set; } 
} 

這裏表市

[Table("cities")] 
public class City 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int id { get; set; } 

    public string name { get; set; } 

    public int state_id { get; set; } 

    [ForeignKey("state_id")] 
    public virtual States states { get; set; } 
    public virtual ICollection<Item> Items { get; set; } 
} 
+0

參見[本DotNetFiddle](https://dotnetfiddle.net/1bPZym)用於實現級聯dropdownlists(特別是在代碼控制器) –

+0

你能顯示你的類別類嗎? – Aravind

+0

我添加了類 –

回答

0

嘗試改變@DropDownList@DropDownListFor(c => c.CountryID, (SelectList)ViewBag.SelectCountry, "select please", new { @class = "form-control" }) 開始形式將看到變化值選項下拉菜單和你的榜樣CountryID將發送到您的控制器

0

問題可能是因爲你可能沒有設置正確導航屬性。 您提及Foriegn鍵的方式有效,但是反向導航丟失。請將以下幾行添加到相應的類中。

public virtual ICollection<Category> Categories { get; set; } 

public virtual ICollection<Country> Countries { get; set; } 

public virtual ICollection<States> States { get; set; } 

public virtual ICollection<City> Cities { get; set; } 

參考此鏈接更多information上實體框架導航屬性和關係

+0

我已經將它添加到相應的類上! 我認爲一定是從jquery?!! –

+0

請更新類別或國家類別的郵政 – Aravind

+0

感謝您的重播好的我現在完成 –

相關問題