简体   繁体   中英

How to bind dropdownlist in mvc 3.0

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

I want to create a form to add document type and in the 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);
      }
}

Controller:

public ActionResult AddDocument()
{
    tbl_DocumentTypes _document = new tbl_DocumentTypes();
    return View(new DocumentTypeViewModel(_document));
}

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'm getting System.Web.Mvc.SelectListItem . Please help.

Change the Module property of your DocumentTypeViewModel to type of IEnumerable<SelectListItem>

public class DocumentTypeViewModel
{
     ModuleRepository _modulerepository = new ModuleRepository();
     public tbl_DocumentTypes Document { get; private set; }
     public IEnumerable<SelectListItem> 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 });
          Modules = _modulerepository.FindAllModules()
                    .Select(d => new SelectListItem
                                 { 
                                    Value= SqlFunctions.StringConvert((double?)d.Module_Id), 
                                    Text = d.ModuleName 
                                 });          
      }
}

And in your view:

<div class="editor-label">Module</div>
<div class="editor-field">
    @Html.DropDownList("Document.Module_Id", Model.Modules)
    @Html.ValidationMessageFor(model => model.Document.Module_Id)
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM