2013-10-04 57 views
2

我試圖插入dropdownlist的值在數據庫中,但不是存儲其選定的值,它是 存儲此值System.Collections.Generic.List1[System.Web.Mvc.SelectListItem]。 這是我的代碼。 查看:DropDownlist值插入爲System.Collections.Generic.List`1 [System.Web.Mvc.SelectListItem]

 @Html.DropDownListFor(m =>m.propertyType, (List<SelectListItem>) ViewData["property"]) 

型號:

public string propertyType { get; set; } 


    [HttpGet] 
    public ActionResult EditProfile() 

    { 
     List<SelectListItem> items = new List<SelectListItem>(); 
     items.Add(new SelectListItem { Text = "Private Residence", Value = "Private Residence" }); 
     items.Add(new SelectListItem { Text = "Office", Value = "Office" }); 
     items.Add(new SelectListItem { Text = "Place of worship", Value = "Place of worship" }); 
     items.Add(new SelectListItem { Text = "Commercial Parking lot", Value = "Commercial Parking lot" }); 
     items.Add(new SelectListItem { Text = "Retail", Value = "Retail" }); 
     items.Add(new SelectListItem { Text = "Academic Institution", Value = "Academic Institution" }); 
     items.Add(new SelectListItem { Text = "Other", Value = "Other" }); 
     ViewData["property"] = items; 




     return View(); 
    } 

    [HttpPost] 

    public ActionResult EditProfile(EditProfile edit) 
    { 
     List<SelectListItem> items = new List<SelectListItem>(); 
     items.Add(new SelectListItem { Text = "Private Residence", Value = "Private Residence" }); 
     items.Add(new SelectListItem { Text = "Office", Value = "Office" }); 
     items.Add(new SelectListItem { Text = "Place of worship", Value = "Place of worship" }); 
     items.Add(new SelectListItem { Text = "Commercial Parking lot", Value = "Commercial Parking lot" }); 
     items.Add(new SelectListItem { Text = "Retail", Value = "Retail" }); 
     items.Add(new SelectListItem { Text = "Academic Institution", Value = "Academic Institution" }); 
     items.Add(new SelectListItem { Text = "Other", Value = "Other" }); 
     ViewData["property"] = items; 
     string UserName = User.Identity.Name; 
     var query = from q in Session.Query<Registration>() 
        where q.Email == UserName 
        select q; 
     if (query.Count() > 0) 
     { 
      foreach (var update in query) 
      { 

       update.contactNo = edit.contactNo; 
       update.occupation = ViewData["property"].ToString(); 
      } 
     } 
     else ModelState.AddModelError("",""); 

     Session.SaveChanges(); 

     return View(); 
    } 

任何幫助將是非常appreciated.Thanks!

+0

你的'編輯'對象是否得到正確的值? – AthibaN

+0

是的,所有的編輯對象都得到了正確的值,除了這個下拉列表值 – Wasfa

回答

0

此行是瘋狂:

update.occupation = ViewData["property"].ToString(); 

應該be:

update.occupation = edit.propertyType; 
1

我總是儘量避免使用ViewData或ViewBag。試試下面這個porpose:

所有的下拉列表中創建靜態類:

public static class MyDrops 
{ 

public static List<SelectListItem> GetList() 
{ 
List<SelectListItem> items = new List<SelectListItem>(); 
     items.Add(new SelectListItem { Text = "Private Residence", Value = "Private Residence" }); 
     items.Add(new SelectListItem { Text = "Office", Value = "Office" }); 
     items.Add(new SelectListItem { Text = "Place of worship", Value = "Place of worship" }); 
     items.Add(new SelectListItem { Text = "Commercial Parking lot", Value = "Commercial Parking lot" }); 
     items.Add(new SelectListItem { Text = "Retail", Value = "Retail" }); 
     items.Add(new SelectListItem { Text = "Academic Institution", Value = "Academic Institution" }); 
     items.Add(new SelectListItem { Text = "Other", Value = "Other" }); 
return items; 
} 
} 
在您查看

然後:

@Html.DropDownListFor(m =>m.propertyType, MyDrops.GetList()) 
+0

我怎樣才能訪問MyDrops類?/ – Wasfa

+0

@Wasfa只需在視圖中添加命名空間:using Projectname.FolderWhereClassSaved –

+0

我試過這一個視圖@using MvcMembership.Models。 MyDrops,但它給出了一個錯誤'MvcMembership.Models.MyDrops'是一個類型不是命名空間 – Wasfa

相關問題