ASP.NET MVC 3.0 - 将MultiSelectList值发布到数据库的问题

时间:2012-07-14 15:47:22

标签: c# asp.net-mvc asp.net-mvc-3

我是MVC的新手,也是编程(一般意义上的)的新手。

我正在努力解决如何根据我从我填充的MultiSelectList中提取的值将多个数据库条目发布到我的数据库(通过我的控制器)的概念。

基本上,我正在尝试创建一个包含State和City的简单模型,并允许用户根据从单独的静态数据库中提取的State / City值,选择多个City值并将其保存到其帐户。 / p>

这是我的模特:

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);
}

根据上面的内容,我根据我的多选列表创建一个集合,将其分解为多个变量,并在重定向之前多次遍历我的database.SaveChanges?

谢谢!

2 个答案:

答案 0 :(得分:0)

这个技巧与你的MVC代码无关。您必须更改业务层或数据访问层(DAL)代码以读取/写入2个不同的数据库。

我做了同样的项目,以前回答一个问题。这是:https://stackoverflow.com/a/7667172/538387

我检查了代码,它仍然有效,您可以下载并自行检查。

答案 1 :(得分:0)

当用户将“创建表单和应用程序流”提交到[HttpPost] public ActionResult Create操作时,所有表单数据都存在于formValues参数中。

您必须从formValues读取这些数据(例如:formValues["companyID"],从中构建适当的模型对象,然后更新数据库。