ASP.net MVC でのドロップダウン リストの使用について質問があります。
これは私の状況です:
ビューを作成:
@using (Html.BeginForm("Create", "Deliverable", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>EventViewModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Thumbnail)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Thumbnail, new { type = "file" })
@Html.ValidationMessageFor(model => model.Thumbnail)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Image)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Image , new {type="file"})
@Html.ValidationMessageFor(model => model.Image)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.VideoUrl)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.VideoUrl)
@Html.ValidationMessageFor(model => model.VideoUrl)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.VideoUrl)
</div>
<div class="editor-field">
@Html.ValidationMessageFor(model => model.VideoUrl)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
「VideoURL」の後に、データベースに保存されている値を含むドロップダウン リストが必要です。私が見つけたチュートリアルのほとんどは、ドロップダウンリストの値をデータベースにバインドしていません。
バックオフィスのデータベースに値を挿入するため、フロントオフィスに挿入できません...
これは私のコントローラーです:
public ActionResult Create(DeliverableViewModel model)
{
var afstudeerrichtingen = (repository.GetAfstudeerrichtingen()).ToList();
if (ModelState.IsValid)
{
try
{
MemoryStream target = new MemoryStream();
model.Image.InputStream.CopyTo(target);
byte[] data = target.ToArray();
model.Thumbnail.InputStream.CopyTo(target);
byte[] datatwo = target.ToArray();
users usr = userrepo.FindByUsername(User.Identity.Name);
model.UsernameID = usr.user_id;
repository.AddDeliverable(model.Title, model.Description, model.UsernameID, data, datatwo, model.VideoUrl, model.Afstudeerrichting);
}
catch (ArgumentException ae)
{
ModelState.AddModelError("", ae.Message);
}
return RedirectToAction("Index");
}
return View(model);
}
値をリポジトリに送信し、データベースに保存します。
これは私のDeliverableViewModelです:
public class DeliverableViewModel
{
[Required]
[Display(Name = "Title")]
public string Title { get; set; }
[Required]
[Display(Name = "Description")]
public string Description { get; set; }
[Required]
[Display(Name = "Thumbnail")]
public HttpPostedFileBase Thumbnail { get; set; }
[Required]
[Display(Name = "Image")]
public HttpPostedFileBase Image { get; set; }
[Required]
[Display(Name = "VideoUrl")]
public string VideoUrl { get; set; }
[Required]
[Display(Name = "AfstudeerrichtingID")]
public int AfstudeerrichtingID { get; set; }
[Required]
[Display(Name = "Afstudeerrichting")]
public IEnumerable<SelectListItem> Items { get; set; }
public long UsernameID { get; set; }
}
これを機能させるためにビューとコントローラーに何を追加する必要があるか知っている人はいますか?
値は、私の mysql データベースの表「Afstudeerrichtingen」にあります
- afstuddeerichting_id
- afstudeerrichting_name