我有一个关于在 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);
}
在将值发送到我的存储库中,我将其保存在我的数据库中。
这是我的可交付视图模型:
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