私は 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; }
}
}
これは私のViewデータのスニペットです(問題なく入力できる都市の複数選択リストを含む):
<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 を複数回ステップ実行する、上記の行に沿ったものですか?
ありがとう!