2012-07-14 34 views
1

我是MVC的新手,也是編程新手(一般意義上的)。ASP.NET MVC 3.0 - 發佈MultiSelectList值到數據庫

我與如何發佈多個數據庫條目到我的數據庫基礎上,我從我已經填充的MultiSelectList拉動值的概念(通過我的控制器)掙扎。

基本上,我試圖創建一個包含州和城市的簡單模型,並允許用戶根據從單獨的靜態數據庫中提取的州/城市值來選擇並保存多個城市值到他們的帳戶。

這裏是我的模型:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.ComponentModel.DataAnnotations; 

namespace BidFetcher.Models 
{ 
public class SPServiceLocation 
{ 
    public int SPServiceLocationID { get; set; } 
    public int SPCompanyAccountID { get; set; } 

    [Display(Name = "State")] 
    public string state { get; set; } 

    [Required] 
    [Display(Name = "Cities (select all that apply)")] 
    public string city { get; set; } 

    public virtual SPCompanyAccount SPCompanyAccount { get; set; } 
} 
} 

這裏是我的查看數據的片段(包括多選列表的城市,我沒有問題填充):

<div class="editor-label"> 
     @Html.LabelFor(model => model.state) 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("state", ViewBag.state as SelectList, "Select a state...", new { @class = "chzn-select", onchange = "CityChange()" }) 
     @Html.ValidationMessageFor(model => model.state) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.city) 
    </div> 

    <div class="editor-field" id="SP_SelectCity"> 
     @Html.ListBox("city", ViewBag.city as MultiSelectList, new { @class = "chzn-select", data_placeholder = "Select cities..." }) 
     @Html.ValidationMessageFor(model => model.city) 
    </div> 

    <p> 
     <input type="submit" value="Submit" /> 
    </p> 

這裏是我的創建/發佈控制器:

public ActionResult Create() 
    { 

     ViewBag.sessionName = HttpContext.Session["SPCompanyName"].ToString(); 

     var compID = HttpContext.Session["SPCompanyAccountID"].ToString(); 

     ViewBag.companyID = compID; 

     ViewBag.state = new SelectList(simpleDB.simpleState, "simpleStateID", "simpleStateID"); 

     ViewBag.city = new MultiSelectList(simpleDB.simpleCity, "cityFull", "cityFull"); 

     return View(); 
    } 

    [HttpPost] 
    public ActionResult Create(SPServiceLocation spservicelocation, FormCollection formValues) 
    { 

      if (ModelState.IsValid) 
      { 

       db.SPServiceLocation.Add(spservicelocation); 
       db.SaveChanges(); 
       return RedirectToAction("Create", "SPServiceLocation"); 
     } 

     return View(spservicelocation); 
    } 

正如你所看到的,我錯過了我的[HttpPost]低我保存多個值到db數據庫,但我不知道它到底是什麼缺少。我已經看到一些解釋這個在創建IEnumerable列表方面的帖子,但我想我只是不確定我需要這樣做,因爲我已經通過數據庫調用成功地填充了我的MultiSelectList。

任何幫助,非常感謝!

編輯:

基於下面的答案,如果我想創建基於我從MultiSelectList收集結果多新的數據庫行,我需要使用Form集合抓住這些結果和解析它們在我的HttpPost:

而且......我不知道該怎麼做。我認爲這些方針的東西:

[HttpPost] 
public ActionResult Create(SPServiceLocation spservicelocation, FormCollection formValues) 
{ 

     if (ModelState.IsValid) 
     { 
      foreach (var item in x) 
      { 
      db.SPServiceLocation.Add(spservicelocation); 
      db.SaveChanges(); 
      } 
      return RedirectToAction("Create", "SPServiceLocation"); 
    } 

    return View(spservicelocation); 
} 

沿上方,在這裏我創建基於我multiselectlist收集的東西線,它分成多個變量,並通過我的database.SaveChanges步驟多次我之前重定向?

謝謝!

+0

你如何想象在單個城市變量中存儲多個城市? – archil 2012-07-14 15:59:31

回答

0

這個技巧與你的MVC代碼無關。您必須更改業務層或數據訪問層(DAL)代碼才能讀取/寫入2個不同的數據庫。

我做了同一個項目來回答前一個問題。那就是:https://stackoverflow.com/a/7667172/538387

我檢查的代碼,它仍然在工作,你可以下載並檢查自己。

+0

感謝Tohid,但我應該澄清一下,我能夠成功地從靜態數據庫中讀取數據,並將其寫入/保存到我的應用程序的數據庫中,但我無法一次爲多個值執行此操作。在這種情況下,上面的代碼在我的數據庫中創建了一條新記錄,但是我只選擇了第一個城市的一條記錄(並且忽略了選擇兩到三,四,五)。我想要做的是爲位於MultiSelectList中的每個選定城市創建一條記錄。 – 2012-07-14 16:28:32

+0

好的。明白了,我會在新帖子中回答你。 – Tohid 2012-07-14 22:07:50

0

當用戶提交創建表單和應用程序流程到[HttpPost] public ActionResult Create操作時,所有表單數據都生存在formValues參數中。

你必須從formValues(如:formValues["companyID"],從它們構建適當的模型對象,然後更新數據庫。

+0

謝謝Tohid!這正是我試圖採取的路徑,關於如何構建MultiSelectList的任何想法? – 2012-07-16 04:59:36

+0

'@ Html.DropDownList()','@ Html.ListBox()'和任何構建表格的Html助手都有一個給定的「名稱」。因此,在[HttpPost]方法中,您可以使用名稱檢索數據;在你的情況:'formValues [「狀態」]和'formValues [「city」]''。 – Tohid 2012-07-17 23:09:42