Dear Sir,
I am new in mvc and i want to bind dropdownlist in MVC 3.0. My Code is given below
tables
tbl_Modules
Module_Id,ModuleName,ModuleDescription
tbl_DocumentTypes
Document_Id,DocumentName,DocumentDescription,
Module_Id
now i want to craete form to add document type and in form i want a dropdownlist with Module_Id as value,ModuleName as text.
public class DocumetRepository
{
InwardManagementEntities db = new InwardManagementEntities();
public IQueryable<tbl_DocumentTypes> FindAllDocumentTypes()
{
return db.tbl_DocumentTypes;
}
}
public class DocumentTypeViewModel
{
ModuleRepository _modulerepository = new ModuleRepository();
public tbl_DocumentTypes Document { get; private set; }
public SelectList Modules { get; private set; }
public DocumentTypeViewModel(tbl_DocumentTypes document)
{
Document = document;
//var _modules = _modulerepository.FindAllModules().Select(d => new {Module_Id= SqlFunctions.StringConvert((double?)d.Module_Id), Text = d.ModuleName });
var _modules = _modulerepository.FindAllModules().Select(d => new SelectListItem() {Value= SqlFunctions.StringConvert((double?)d.Module_Id), Text = d.ModuleName });
Modules = new SelectList(_modules, Document.Module_Id);
}
}
And in conroller
public ActionResult AddDocument()
{
tbl_DocumentTypes _document = new tbl_DocumentTypes();
return View(new DocumentTypeViewModel(_document));
}
and in view
<div class="editor-label">
Module
</div>
<div class="editor-field">
@Html.DropDownList("Document.Module_Id", Model.Modules.ToList())
@Html.ValidationMessageFor(model => model.Document.Module_Id)
</div>
but in dropdownlist i get System.Web.Mvc.SelectListItem . Please help me