0

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

2
  • Are you just asking how to create a DropDownList based on data you're getting back from your database? Commented May 30, 2013 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. Commented May 30, 2013 at 20:52

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Will try it at home!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.