ASP.NET MVC 5 で開発を行っています。
下記のように、DB から取得した DataTable のデータを、foreach を使ってテーブルレイアウトにしています。
C#
1@foreach (DataRow dr in Model.DT.Rows) 2{ 3 <tr> 4 <td>@dr["CD"]</td> 5 <td> 6 @Html.DropDownListFor(m => m.SelectedValue, m.data) 7 </td> 8 </tr> 9}
当然ながら、この方法だとすべて同じドロップダウンリストが作成されてしまいます。
前提・実現したいこと
ドロップダウンリストをそれぞれ個別の物として作成し、選択された値を Controller に送れるようにしたいです。
MVC のグリッドは使わず、上記のような方法で実現したいですが、できるのでしょうか?
よろしくお願い致します。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/09/06 01:01
回答1件
0
ベストアンサー
コメントに返事がないのであまり深く考えていませんが・・・
ドロップダウンリストをそれぞれ個別の物として作成し、選択された値を Controller に送れるようにしたいです。
以下の記事の図にあるようなことをしたいのでしょうか?
スキャフォールディング機能
http://surferonwww.info/BlogEngine/post/2017/07/23/creating-controller-and-view-in-mvc-using-scaffolding-function.aspx
であれば、一度上に紹介した記事のとおり、Visual Studio のスキャフォールディング機能を使って作ってみてはいかがですか。
スキャフォールディング機能を使って自動生成された Create の Controller と View のコードを以下に参考にアップしておきます。
Controller / Action Method
using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using Mvc5App; namespace Mvc5App.Controllers { public class ProductsController : Controller { private NORTHWINDEntities db = new NORTHWINDEntities(); // GET: Products/Create public ActionResult Create() { string name = User.Identity.Name; ViewBag.UserName = name; ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName"); ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "CompanyName"); return View(); } // POST: Products/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued")] Products products) { if (ModelState.IsValid) { db.Products.Add(products); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", products.CategoryID); ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "CompanyName", products.SupplierID); return View(products); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }
View
@model Mvc5App.Products @{ ViewBag.Title = "Create"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Products</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) これは <a href="https://teratail.com/questions/135216">Teratail のスレッド</a> の検証用 @Html.TextBox("username", (string)ViewBag.UserName, new { @class = "form-control" }) <br /> <div class="form-group"> @Html.LabelFor(model => model.ProductName, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.ProductName, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.SupplierID, "SupplierID", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("SupplierID", null, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.SupplierID, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.CategoryID, "CategoryID", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("CategoryID", null, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.QuantityPerUnit, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.QuantityPerUnit, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.QuantityPerUnit, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.UnitPrice, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.UnitPrice, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.UnitPrice, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.UnitsInStock, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.UnitsInStock, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.UnitsInStock, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.UnitsOnOrder, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.UnitsOnOrder, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.UnitsOnOrder, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.ReorderLevel, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.ReorderLevel, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.ReorderLevel, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Discontinued, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> <div class="checkbox"> @Html.EditorFor(model => model.Discontinued) @Html.ValidationMessageFor(model => model.Discontinued, "", new { @class = "text-danger" }) </div> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
投稿2018/09/06 09:01
編集2018/09/06 09:02退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。