【问题标题】:ASP.NET MVC 3.0 - Issue posting MultiSelectList values to databaseASP.NET MVC 3.0 - 将 MultiSelectList 值发布到数据库的问题
【发布时间】:2012-07-14 15:47:21
【问题描述】:

我是 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; }
}
}

这是我的视图数据的 sn-p(包括我可以轻松填充的城市的多选列表):

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

类似于上述内容,我根据我的多选列表创建一个集合,将其分解为多个变量,然后在我重定向之前多次遍历我的数据库。SaveChanges?

谢谢!

【问题讨论】:

  • 您如何想象将多个城市存储在单个字符串 City 变量中?

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


【解决方案1】:

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

前段时间我做了同样的项目来回答一个问题。这里是:https://stackoverflow.com/a/7667172/538387

我检查了代码,它仍然可以工作,你可以下载它自己检查。

【讨论】:

  • 感谢 Tohid,但我应该澄清一下,我能够成功地从静态数据库中读取数据并将其写入/保存到我的应用程序的数据库中,但我无法同时为多个值执行此操作.在这种情况下,上面的代码在我的数据库中创建了一条新记录,但只为我选择的第一个城市创建了一条记录(并忽略了从二到三、四、五的选择)。我想要做的是为位于 MultiSelectList 中的每个选定城市创建一条记录。
  • 好的。知道了,我会在新的帖子中回复你。
【解决方案2】:

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

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

【讨论】:

  • 感谢托希德!这正是我试图采取的路径,关于我将如何为 MultiSelectList 构建它的任何想法?
  • @Html.DropDownList()@Html.ListBox() 和任何构建表单 元素的 Html Helper 都有一个给定的“名称”。因此在[HttpPost] 方法中,您可以使用它们的名称检索数据;在您的情况下:formValues["state"]formValues["city"]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多