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:
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; }
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; }
public virtual MyUser User { get; set; }
}
}
Document Create View:
@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:
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);
}