Search code examples
asp.net-mvc-5html-helperdropdownlistfor

DropDownListFor in an MVC ASP application


I have a view model which you see below

public class AssetCategoryViewModel : IEnumerable<AssetCategory>
{
    DataModelContext db = new DataModelContext();
    public AssetCategory AssetCategory { get; set; }
    public Guid Id { get; set; }
    public IList<AssetCategory> AssetCategoryList { get; set; }
    public IEnumerable<SelectListItem> AssetCategoryNames { get; set; }
    public AssetCategoryViewModel()
    {
        AssetCategoryList = (from x in db.AssetCategory
                             select x).ToList();
        AssetCategoryNames = (from x in db.AssetCategory
                              select new SelectListItem
                              {
                                  Text = x.Name,
                                  Value = x.Code
                              });
    }

    public IEnumerator<AssetCategory> GetEnumerator()
    {
        return AssetCategoryList.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

and I want to send SelectlistItem to to view for using dropdownlist.

action method :

    public ActionResult AddNewCategory()
    {
        AssetCategoryViewModel acvm = new AssetCategoryViewModel();

        return View(acvm);
    }

and div class (included dropdownlistfor helper method)

                <div class="form-group">
                    @Html.LabelFor(model => model.AssetCategory.MasterCategory, htmlAttributes: new { @class = "control-label" })
                    @Html.DropDownListFor(model => model.AssetCategoryNames, Model.AssetCategoryNames, "Seçim yapınız", htmlAttributes: new { @class = "form-control" })                        
                </div>

and I got error

Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

I am stuck on this.


Solution

  • I reccomend you to do this way to make dropdownlistfor Instead of using view model you create a model class and add a function which retrieves the data from the database

    your model class should be like,`

    public list<selectListItem> function()
    {
        AssetCategoryList = (from x in db.AssetCategory
                                     select x).ToList();
                AssetCategoryNames = (from x in db.AssetCategory
                                      select new SelectListItem
                                      {
                                          Text = x.Name,
                                          Value = x.Code
                                      });`
    }
    return list;
    

    and it should return the value as list.

    then the controller part should be

     public ActionResult AddNewCategory()
        {
            AssetCategoryModel acm = new AssetCategoryModel();
    viewmodel vm=new viewmodel();
    vm.assetcatagorylist=acm.function();
    
            return View("viewname",vm);
        }
    

    then you should make to render the data to the view by passing the viewmodel data to ur view,

    your view be like

    @using projectname.ViewModel
    @model ViewModel
    <html>
    
    //your design part
    
         @Html.DropDownListFor(m => m.assetcatagorylist, Model.assetcatagorylist, new { @id ="hi" })
    
    
    </html>
    

    IT WORKS PERFECTLY CHEERS,