2013-05-30 131 views
0

我有一个关于在ASP.net MVC中使用下拉列表的问题。从数据库绑定数据到下拉列表+ 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”之后,我想有一个下拉列表与存储在我的数据库值。我发现的大多数教程都没有将下拉列表中的值绑定到数据库。

我插入我的分贝值在我的后台,所以我不能在frontoffice插入他们...

这是我的控制器:

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; } 
} 

有谁知道我必须在我看来&控制器添加,使这项工作?

值都在我的TABEL “Afstudeerrichtingen” 在我的MySQL数据库
- afstuddeerichting_id
- afstudeerrichting_name

+0

你只是问如何根据你从数据库中获得的数据创建一个DropDownList? – JustinMichaels

+0

是的,也许你有一个很好的教程,或者如此?我现在从存储库中获取我的数据。 – nielsv

回答

1

在视图中添加一个下拉列表:

@Html.DropDownListFor(model => model.AfstudeerrichtingID, 
    new SelectList(ViewBag.Richtingen, 
    "AfstudeerrichtingId", "AfstudeerrichtingName")) 

您Afstudeerrichting集合到ViewBag在控制器中:

ViewBag.Richtingen = afstudeerrichtingen; 

假设该集合包含视图中使用的属性AfstudeerrichtingId和AfstudeerrichtingName。

要做的另一件事是将您的AfstudeerrichtingID更改为字符串,并将其翻译到您的资源库类中的int。

+0

谢谢!将在家中尝试! – nielsv

相关问题