2013-05-30 2 views
0

ASP.net MVC에서 드롭 다운 목록 사용에 관한 질문이 있습니다.db from dropdownlist + 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); 
    } 

에서 내 저장소 위치로 값을 전송 : 나는 frontoffice에 삽입 할 수 있도록

나는

내 컨트롤러입니다 ... 내 백 오피스에서 내 DB에 값을 삽입 내 데이터베이스에 저장합니다.

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

사람이 내가이 일을 내보기 & 컨트롤러에 추가 할 필요가 무엇인지 알고 있나요? afstuddeerichting_id
- -

값은 내 MySQL 데이터베이스에 내 tabel "Afstudeerrichtingen"에
있습니다 afstudeerrichting_name

+0

데이터베이스에서 돌아 오는 데이터를 기반으로 DropDownList를 만드는 방법을 묻는 중입니까? – JustinMichaels

+0

네, 어쩌면 좋은 튜토리얼을 가지고 있을까요? 이제 저장소에서 얻은 목록에 데이터를 가져옵니다. – nielsv

답변

1

이보기에서 드롭 다운리스트를 추가

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

을 ViewBag에 Afstudeerrichting 모음 추가 컨트롤러에서 :

ViewBag.Richtingen = afstudeerrichtingen; 

해당 컬렉션에 뷰에 사용 된 AfstudeerrichtingId 및 AfstudeerrichtingName 속성이 포함되어 있다고 가정합니다.

또 다른해야 할 일은 AfstudeerrichtingID를 string으로 변경하고 저장소 클래스의 int에서 /로 변환하는 것입니다.

+0

감사합니다. 집에서 그것을 시도 할 것이다! – nielsv