Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I changed my model class (Document), and i inserted one dropdownlist to my Playlist propertie. But, the Bind method of my object dont working fine for this situation.

the problem is: my Playlist propertie value comes NULL into Create method.

Document Model:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace Desenvolvimento.Models
{
    public enum DocumentCategory
    {
        Photo = 0,
        Music = 1,
        Video = 2,
        Other = 3
    }

    public class Document
    {   
        [Key]
        public int Id { get; set; }

        [Required]
        [Display(Name = "Nome")]
        public string Name { get; set; }

        [Display(Name = "Descrição")]
        public string Description { get; set; }

        //[Required]
        public string Path { get; set; }

        [Required]
        [Display(Name = "Categoria")]
        public DocumentCategory Category { get; set; }
        
        [Display(Name="Privado")]
        public bool isPrivate { get; set; }

        [Display(Name = "Playlist")]
        public virtual Playlist Playlist { get; set; }
        
        //[Required]
        public virtual MyUser User { get; set; }
    }
    
}


Document Create View:
C#
@model Desenvolvimento.Models.Document

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";       
}

<h2>Create</h2>


@using (Html.BeginForm("Create", "Document", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Document</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Description, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Description)
                @Html.ValidationMessageFor(model => model.Description)
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-md-2">Selecione o arquivo:</label>
            @*@Html.LabelFor(model => model.Path, new { @class = "control-label col-md-2" })*@
            <div class="col-md-10">
                @*@Html.EditorFor(model => model.Path)
                @Html.ValidationMessageFor(model => model.Path)*@
                <input type="file" name="upload" class="btn btn-default" />

            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Category, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @{
                    List<SelectListItem> listItems = new List<SelectListItem>();
                    listItems.Add(new SelectListItem
                         {
                             Text = "Fotos",
                             Value = "Photo"
                         });
                    listItems.Add(new SelectListItem
                         {
                             Text = "Vídeos",
                             Value = "Video"
                         });
                    listItems.Add(new SelectListItem
                         {
                             Text = "Músicas",
                             Value = "Music"
                         });
                    listItems.Add(new SelectListItem
                        {
                            Text = "Outros",
                            Value = "Other"
                        });
                }

                @Html.DropDownListFor(model => model.Category, listItems, "-- Selecione a Categoria --", new { @class = "select" })
                @Html.ValidationMessageFor(model => model.Category)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Playlist, new { @class = "control-label col-md-2" })
            <div class="col-md-10">               
                @Html.DropDownListFor(model => model.Playlist, ((Desenvolvimento.Controllers.DocumentController)this.ViewContext.Controller).GetPlaylists(), "-- Selecione a Playlist --", new { @class = "select" })  
                @Html.ValidationMessageFor(model => model.Playlist)              
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.isPrivate, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.isPrivate)
                @Html.ValidationMessageFor(model => model.isPrivate)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}



Document Controller:
C#
//return dropdownlist
 public List<SelectListItem> GetPlaylists()
        {
            List<SelectListItem> playlists = new List<SelectListItem>();

            try
            {
                playlists.Add(new SelectListItem { Text = "-- Selecione a Playlist --", Value = "0" });
                foreach (var item in db.Playlists)
                {
                    playlists.Add(new SelectListItem { Text = item.Name, Value = Convert.ToString(item.Id) });
                }
            }
            catch (Exception)
            {                
                throw;
            }

            return playlists;            
        }


 [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "Id,Name,Description,Path,Category,isPrivate,Playlist,Album")] Document document)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    
                    db.Documents.Add(document);
                    await db.SaveChangesAsync();
                }
                catch (Exception)
                {
                    throw;
                }

                return RedirectToAction("Index");
            }


           return View(document);
        }
Posted

SQL
Try bind dropdownlist on following way,it may help you

@Html.DropDownList("Playlist",((Desenvolvimento.Controllers.DocumentController)this.ViewContext.Controller).GetPlaylists(), "-- Selecione a Playlist --",  new { @class = "select" })  
 
Share this answer
 
Comments
EduChapow 18-Mar-14 10:27am    
thanks man, i going try at night.
but, why the Category dropdownlist works fine and Playlist not?
EduChapow 18-Mar-14 19:15pm    
did not work!!! =S
my model is updated, i need do something to work?
 
Share this answer
 
Comments
EduChapow 18-Mar-14 10:27am    
thanks man, i going try at night.
but, why the Category dropdownlist works fine and Playlist not?
EduChapow 18-Mar-14 19:18pm    
hey @Charan_Kumar, my problem not is bind dropdownlist! my problem is to get selected value from a dropdownlist.
Charan_Kumar 19-Mar-14 0:58am    
try this

http://www.codeproject.com/Answers/737834/How-will-I-get-dddlsongs-dropdownlist-selected-val#answer1
EduChapow 20-Mar-14 15:16pm    
it's not help me. =S

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900