2015-12-04 40 views
0

我已經接近將我的內容放入數據庫,但是當我試圖將其放入數據庫以便它絕不允許自己做到它成。從我的SelectListItem中獲取值,以便它可以進入數據庫

在編寫此處錯誤時沒有獲得數據庫值的問題。

無法將類型爲'System.Collections.Generic.List`1 [System.Web.Mvc.SelectListItem]'的對象轉換爲鍵入'System.IConvertible'。

這裏

[HttpPost] 
    [AllowAnonymous] 
    public ActionResult create(AccountViewModels userindhold) 
    { 
     DataLinqDB db = new DataLinqDB(); 
     if (!ModelState.IsValid) 
     { 
      var username = db.brugeres.FirstOrDefault(i => i.brugernavn == userindhold.Brugernavn); 
      if (username == null) 
      { 
       brugere OpretUser = new brugere(); 
       OpretUser.kon = Convert.ToInt32(userindhold.Kon);//error her 
       OpretUser.birthday = Convert.ToDateTime(userindhold.Birthday);//error her 
       OpretUser.nyhedsbrevKategori_Id = Convert.ToInt32(userindhold.KategoriNyhedsbrev);//error her 

       db.brugeres.InsertOnSubmit(OpretUser); 

       db.SubmitChanges(); 

       return RedirectToAction("login"); 
      } 
      else 
      { 
       ModelState.AddModelError("", "Denne e-mail er genkendelig."); 
      } 

     } 
     return View(); 
    } 

後獲取

[HttpGet] 
    [AllowAnonymous] 
    public ActionResult create() 
    { 
     DataLinqDB db = new DataLinqDB(); 

     AccountViewModels ac = new AccountViewModels(); 

     ViewBag.Kon = new SelectList(db.Kons, "id", "konvalue"); 
     ViewBag.KategoriNyhedsbrev = new SelectList(db.KategoriNyhedsbrevs, "id", "tekst"); 

     return View(); 
    } 

Modelse這裏

[Display(Name = "Hvilken kategori kan du li til nyhedsbrevet?")] 
    public IEnumerable<SelectListItem> KategoriNyhedsbrev 
    { 
     get; set; 
    } 

    [Display(Name = "Hvilke køn er du?")] 
    public IEnumerable<SelectListItem> Kon 
    { 
     get; set; 
    } 

我在這裏有一個表格,這裏有更多的輸入區域。

index.cshtml

<div class="form-group"> 
      <div class="col-xs-12"> 
       @Html.LabelFor(u => u.Kon) 
       @Html.DropDownList("Kon", null, new 
      { 
       @class = "form-control", 
      }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-xs-12"> 
       @Html.LabelFor(u => u.KategoriNyhedsbrev) 
       @Html.DropDownList("KategoriNyhedsbrev", null, new 
      { 
       @class = "form-control", 
      }) 
      </div> 
     </div> 

回答

0

在你AccountViewModel,KonIEnumerable<SelectListItem>類型的屬性,你試圖傳遞到Convert.ToInt32方法。 Convert.ToInt32通常需要字符串版本的數字數據(例如:"34"),以便可以安全地將其轉換爲整數版本(例如:34)。此方法無法將列表轉換爲整數值。

什麼,你應該最好做的是一個屬性添加到您的視圖模型來保存所選的項目值

public class AccountViewModel 
{ 
    public int SelectedKon {set;get;} 
    public List<SelectListItem> Kon { get; set; } 

    //Your other properties also goes here 
} 

而在你的Razor視圖,

@model AccountViewModel 
@using(Html.BeginForm()) 
{ 
    @Html.DropDownListFor(s=>s.SelectedKon, Model.Kon,"Select one item") 
    <input type="submit" /> 
} 
在HttpPost行動

現在,您可以使用SelectedKon發佈模型的屬性值

[HttpPost] 
public ActionResult Create(AccountViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     brugere OpretUser = new brugere(); 
     OpretUser.kon = model.SelectedKon; 
     //Do other useful things and redirect to a Success page. 
    } 
    //TO DO : Reload the model.Kon collection again here. 
    return View(model) 
} 

您應該對所有的下拉菜單都一樣。

+0

感謝您的好評。但它在這裏顯示錯誤。 's => s.SelectedKon' –

+0

您是否已將該屬性添加到您的視圖模型中? – Shyju

+0

我將它添加到我已拍攝的照片中,您可以在此處看到。 http://postimg.org/image/yepf5dahx/ –

相關問題