質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

ASP.NET

ASP.NETは動的なWebサイトやWebアプリケーション、そしてWebサービスを構築出来るようにする為、Microsoftによって開発されたウェブアプリケーション開発フレームワークです。

ASP.NET MVC 4

ASP.NET MVC4は、MVCパターンを利用して、高度なテスト機能と保守機能を備えた Web アプリケーションを開発するためのフレームワークです。

Q&A

解決済

1回答

10568閲覧

ASP.NET MVCでDropDownListを動的に作成する方法

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

ASP.NET

ASP.NETは動的なWebサイトやWebアプリケーション、そしてWebサービスを構築出来るようにする為、Microsoftによって開発されたウェブアプリケーション開発フレームワークです。

ASP.NET MVC 4

ASP.NET MVC4は、MVCパターンを利用して、高度なテスト機能と保守機能を備えた Web アプリケーションを開発するためのフレームワークです。

0グッド

0クリップ

投稿2018/09/05 15:27

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 00:59

> ドロップダウンリストをそれぞれ個別の物 ← そこのところ、もっと具体的に、どのように「個別」にしたいのか書いてください。DataTable の中身のなにかを「個別」に表示したいように思えますが、それにしても何だか分かりません。もっと前に戻って、DataTable を使うのが適切かも考えた方がよさそうです(XY 問題に陥ってませんか?)。
退会済みユーザー

退会済みユーザー

2018/09/06 01:01

あと、MVC5 だそうですが MVC4 のタグは不適切です。外してください。他に ASP.NET MVC Framework というタグがありますので、MVC ということをタグで示したいならそれを使ってください。
guest

回答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

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問