ASP.NET mvc5 を用いて、掲示板を作成している初学者です。
質問に対する回答、いつもありがとうございます。
前提・実現したいこと
DBfirstでの"ADO.NET Entity Data Model"の作成機能を用いてModelを作成し、スキャフォールディング機能を用いてViewとControllerの自動生成をしました。
その後、ASP.NET identityを用いて実装したログイン機能を基に、トピックの中のレスの作成をしているのですが、行き詰まったので質問を投稿しました。
発生している問題・エラーメッセージ
次の画像はトピックの中のレスを新規に作成するためのページなのですが、以下の問題を抱えています。
(1) レスの記入者を現在ログインしているユーザー名で固定したいが、ユーザー名をすべて表示してしまう。また、世の中の目標制作物に類似するものはそもそも「記入者=ログインユーザー名」だというのが自明なので表示してないように思えるので表示しない方法もあればご教授いただけたら幸いです。
該当のソースコード
Controller
C#
1 // GET: Responses/Create 2 public ActionResult Create() 3 { 4 string id = User.Identity.GetUserId(); 5 string name = User.Identity.GetUserName(); 6 ViewBag.ResOwnerId = new SelectList(db.AspNetUsers, "Id", "UserName"); 7 8 ViewBag.ResId = new SelectList(db.Responses, "Id", "Title"); 9 ViewBag.TopicId = new SelectList(db.Topics, "Id", "Title"); 10 return View(); 11 // return Content(id + name); 12 } 13 14 // POST: Responses/Create 15 // 過多ポスティング攻撃を防止するには、バインド先とする特定のプロパティを有効にしてください。 16 // 詳細については、https://go.microsoft.com/fwlink/?LinkId=317598 を参照してください。 17 [HttpPost] 18 [ValidateAntiForgeryToken] 19 public ActionResult Create([Bind(Include = "Id,")] Responses responses) 20 { 21 if (ModelState.IsValid) 22 { 23 db.Responses.Add(responses); 24 db.SaveChanges(); 25 return RedirectToAction("Index"); 26 } 27 28 ViewBag.ResOwnerId = new SelectList(db.AspNetUsers, "Id", "Email", responses.ResOwnerId); 29 ViewBag.ResId = new SelectList(db.Responses, "Id", "Title", responses.ResId); 30 ViewBag.TopicId = new SelectList(db.Topics, "Id", "Title", responses.TopicId); 31 return View(responses); 32 }
View
C#
1@model AlbatrossOnClouds.Models.Responses 2 3@{ 4 ViewBag.Title = "Create"; 5} 6 7<h2>Create</h2> 8 9 10@using (Html.BeginForm()) 11{ 12 @Html.AntiForgeryToken() 13 14 <div class="form-horizontal"> 15 <h4>Responses</h4> 16 <hr /> 17 @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 18 19 <div class="form-group"> 20 @Html.LabelFor(model => model.TopicId, "トピック名", htmlAttributes: new { @class = "control-label col-md-2" }) 21 <div class="col-md-10"> 22 @Html.DropDownList("TopicId", null, htmlAttributes: new { @class = "form-control" }) 23 @Html.ValidationMessageFor(model => model.TopicId, "", new { @class = "text-danger" }) 24 </div> 25 </div> 26 27 28 </div> 29 30 31 32 <div class="form-group"> 33 @Html.LabelFor(model => model.Contents, htmlAttributes: new { @class = "control-label col-md-2" }) 34 <div class="col-md-10"> 35 @Html.EditorFor(model => model.Contents, new { htmlAttributes = new { @class = "form-control" } }) 36 @Html.ValidationMessageFor(model => model.Contents, "", new { @class = "text-danger" }) 37 </div> 38 </div> 39 40 41 <div class="form-group"> 42 @Html.LabelFor(model => model.ResOwnerId, "レスの記入者", htmlAttributes: new { @class = "control-label col-md-2" }) 43 <div class="col-md-10"> 44 45 @Html.DropDownList("ResOwnerId", null, htmlAttributes: new { @class = "form-control" }) 46 @Html.ValidationMessageFor(model => model.ResOwnerId, "", new { @class = "text-danger" }) 47 48 </div> 49 </div> 50 51 52 <div class="form-group"> 53 <div class="col-md-offset-2 col-md-10"> 54 <input type="submit" value="Create" class="btn btn-default" /> 55 </div> 56 </div> 57 </div> 58} 59 60<div> 61 @Html.ActionLink("Back to List", "Index") 62</div> 63 64@section Scripts { 65 @Scripts.Render("~/bundles/jqueryval") 66} 67
試したこと
(1)
Controllerを次のように書き換えたところ、次の画像のようにViewで表示されるDropDownListの中身がChar型になってしまいます。
return contentを用いてidとnameを表示した時にはハッシュ値のパスとユーザー名が表示できたのでidとnameには問題がないのではと考えています
(idとnameにIdentityの情報を格納し、Viewbag.ResOwnerId の値をコメントアウトしている行からその次の行のように書き換えました)
c#
1 // GET: Responses/Create 2 public ActionResult Create() 3 { 4 string id = User.Identity.GetUserId(); 5 string name = User.Identity.GetUserName(); 6 //ViewBag.ResOwnerId = new SelectList(db.AspNetUsers, "Id", "UserName"); 7 ViewBag.ResOwnerId = new SelectList(id); 8 9 ViewBag.ResId = new SelectList(db.Responses, "Id", "Title"); 10 ViewBag.TopicId = new SelectList(db.Topics, "Id", "Title"); 11 return View(); 12 }
補足情報(FW/ツールのバージョンなど)
Windows7
VS2017
ASP.NET MVC5
Net Framework 4.6.1
回答1件
あなたの回答
tips
プレビュー