[英]How to loop through MultiSelectList posted values and insert each into a new database row in ASP.Net MVC 5?
[英]ASP.NET MVC 3.0 - Issue posting MultiSelectList values to database
我是MVC的新手,也是編程的新手(一般而言)。
我在如何根據從已填充的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);
}
類似於上面的內容,我在其中基於多重選擇列表創建一個集合,將其分解為多個變量,然后逐步遍歷數據庫。在重定向之前多次保存更改?
謝謝!
這個技巧與您的MVC代碼無關。 您必須更改業務層或數據訪問層(DAL)代碼才能對2個不同的數據庫進行讀/寫。
不久前,我做了相同的項目來回答一個問題。 在這里是: https : //stackoverflow.com/a/7667172/538387
我檢查了代碼,它仍在工作,您可以下載並自己檢查。
當用戶提交創建表單並將應用程序流程提交到[HttpPost] public ActionResult Create
操作時,所有表單數據都位於formValues
參數中。
您必須從formValues
讀取這些數據(例如: formValues["companyID"]
,從它們中構建適當的模型對象,然后更新數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.