2013-03-05 48 views
0

我在這裏搜索了很多,找到這樣的事情,使下拉列表
這是我的模型:mvc4下拉列表插入空

public class Profits 
    { 
     [Key] 
     public int Id { get; set; } 
     public double Value { get; set; } 
     public string Description{ get; set; } 
     [DataType(DataType.Date)] 
     public DateTime DateInput { get; set; } 
     public UserProfile User { get; set; } 
     public Categories CategoryName { get; set; }//foreign key 
      public int CategoryID { get; set; } 
    } 

public class Categories 
    { 
     [Key] 
     public int Id { get; set; } 
     [Required] 
     public string Name { get; set; } 
     public UserProfile User { get; set; } 


    } 

這是我的控制器: 這把我的數據下拉列表...

public ActionResult Create() 
{ 
    var dba = new WHFMDBContext(); 
    var query = dba.Categories.Select(c => new { c.Id, c.Name }); 
    ViewBag.Id = new SelectList(query.AsEnumerable(), "Id", "Name", 3); 
    return View(); 
} 

[HttpPost] 
     [InitializeSimpleMembership] 
     public ActionResult Create(Profits profits) 
     { 
      var user = db.UserProfiles.FirstOrDefault(x => x.UserId == WebSecurity.CurrentUserId); 
      var profit = new Profits 
      { 
       Value= profits.Value, 
       Description = profits.Description, 
       DateInput =profits.DateInput, 
       CategoryName =profits.CategoryName,// ??? 
       User = user, 

      }; 
      db.Profits.Add(profit); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

筆者認爲:

@model WebHFM.Models.Profits 



@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>Profits</legend> 


    <div class="editor-field"> 
    @Html.LabelFor(model => model.CategoryName) 
     </div> 
    <div class="editor-field"> 
     @Html.DropDownList("Id", (SelectList) ViewBag.Id, "--Select One--") 
     @Html.ValidationMessageFor(model => model.CategoryName) 
    </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Value) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Value) 
      @Html.ValidationMessageFor(model => model.Value) 
     </div>... 

這將數據插入數據庫,但CategoryName_IdNULL我錯過了什麼? 和CategoryName = profits.CategoryName這是一個外鍵關鍵詞利潤public Categories CategoryName { get; set; }

+0

的問題是:我在下拉列表看到數據從db.Categories但是當我選擇從這個DDL一些選項,並增加對其他組件有些價值(來自EditorFor)。他們已經插入數據庫沒有選擇類別從ddl而不是類別我有NULL.Thanks耐心 – Krasimir 2013-03-05 21:06:01

+0

您的利潤類是否有CategoryName或CategoryID。在你的例子中它是CategoryName,但是你設置你的dropdownlist返回的是什麼? – 2013-03-05 21:13:58

+0

是類別CategoryName是一個類別列表還是一個類別? – 2013-03-05 21:28:42

回答

1

修改你的利潤類,補充一點:

Profits 
{ 
    int CategoryID {get;set;} 
} 

修改CSHTML。更改

@Html.DropDownList("Id", (SelectList) ViewBag.Id, "--Select One--") 

@Html.DropDownList("CategoryID", (SelectList) ViewBag.Id, "--Select One--") 

改變你的控制器:

public ActionResult Create(Profits profits) 
     { 
      var user = db.UserProfiles.FirstOrDefault(x => x.UserId == WebSecurity.CurrentUserId); 
      var category = db.Categories.FirstOrDefault(o => o.CategoryId == profits.CategoryID); 
      var profit = new Profits 
      { 
       Value= profits.Value, 
       Description = profits.Description, 
       DateInput =profits.DateInput, 
       CategoryName = category, 
       User = user 
      }; 
      db.Profits.Add(profit); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

這是什麼會做的是改變你的下拉列表,返回所選擇的類別ID。標記將如下所示:

<select name="CategoryID" /> 

在您的回傳中,它將綁定到您的新Profits.CategoryID。然後,您可以使用它來進行數據庫查詢,以獲取您分配給Profits.CategoryName的選定類別對象。

+0

好吧,我現在看到。你可以用@ Html.Textbox()替換你的@ Html.EditorFor()就是我在這裏說的。由於您未傳入模型,因此您將在model.CategoryName上獲得nullref。 – 2013-03-05 20:44:07

+0

在Profits.CategoryName中是一個類別列表還是一個單獨的類別? – 2013-03-05 21:24:24

+0

這是正確的數據庫工作。感謝我知道看到categoryId但在index.cshtml我看到categoryId「0」 – Krasimir 2013-03-05 22:06:11

0

你的Create()行爲返回return View();,而沒有經過模型眼簾的方法參數。

+0

和這裏應該有什麼,因爲它不是一個查看(查詢)我認爲 – Krasimir 2013-03-05 20:40:52

0

這是我建議不看你的模型。 PopulateCategoriesDropDownList住在同一個控制器作爲您創建行動

private void PopulateCategoriesDropDownList(object selectedCategories = null) 
    { 
     var categoriesQuery = from d in _dataSource.Categories 
         orderby d.Name 
         select d; 
     ViewBag.CategoryId= new SelectList(categoriesQuery, "CategoryId", "Name", selectedCategories); 
    } 

然後你的動作是這樣的。

[HttpGet] 
    public ActionResult Create() 
    { 
     PopulateCategoriesDropDownList(); 
    return View(); 
    } 


    [HttpPost] 
    [InitializeSimpleMembership] 
    public ActionResult Create(Profits profits) 
    { 
     var user = db.UserProfiles.FirstOrDefault(x => x.UserId ==  WebSecurity.CurrentUserId); 
     var profit = new Profits 
     { 
      Value= profits.Value, 
      Description = profits.Description, 
      DateInput =profits.DateInput, 
      CategoryName =profits.CategoryName,// ??? 
      User = user, 

     }; 
     db.Profits.Add(profit); 
     db.SaveChanges(); 
     PopulateCategoriesDropDownList(profit.CategoryId); 
     return View(profit); 
    } 

在你看來:

<div class="editor-label"> 
      @Html.LabelFor(model => model.CategoryId, "Pick Category") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("CategoryId", String.Empty) 
     @Html.ValidationMessageFor(model => model.CategoryId) 
    </div><br/>