Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a question about using a dropdownlist in ASP.net MVC.

This is my situation:

Create view:

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

After the 'VideoURL' I would like to have a Dropdownlist with values that are stored in my database. Most of the tutorials that I find don't bind the values in the dropdownlist to the database.

I insert the values in my db in my backoffice so I can't insert them in frontoffice...

This is my Controller:

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

In sent the values to my repository where I save it in my database.

This is my 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; }
}

Does anyone know what I have to add in my view & controller to make this work?

The values are in my tabel "Afstudeerrichtingen" in my mysql database
- afstuddeerichting_id
- afstudeerrichting_name

share|improve this question
    
Are you just asking how to create a DropDownList based on data you're getting back from your database? –  JustinMichaels May 30 '13 at 20:45
    
Yes, maybe you have a good tutorial or so? I now get my data in a list I get from the repository. –  nielsv May 30 '13 at 20:52

1 Answer 1

up vote 1 down vote accepted

Add a dropdownlist in your view:

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

Add your Afstudeerrichting collection to the ViewBag in the controller:

ViewBag.Richtingen = afstudeerrichtingen;

Assuming that that collection contains the properties AfstudeerrichtingId and AfstudeerrichtingName, as used in the view.

Another thing to do is change your AfstudeerrichtingID to string, and translate it to/from int in your repository class.

share|improve this answer
    
Thanks! Will try it at home! –  nielsv May 31 '13 at 7:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.