我有一个关于在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
答案 0 :(得分:1)
在视图中添加下拉列表:
@Html.DropDownListFor(model => model.AfstudeerrichtingID,
new SelectList(ViewBag.Richtingen,
"AfstudeerrichtingId", "AfstudeerrichtingName"))
将Afstudeerrichting集合添加到控制器中的ViewBag:
ViewBag.Richtingen = afstudeerrichtingen;
假设该集合包含AfstudeerrichtingId和AfstudeerrichtingName属性,视图中使用。
要做的另一件事是将AfstudeerrichtingID更改为字符串,并将其转换为存储库类中的int。