T2020 Jackson
T2020 Jackson

Reputation: 17

DropdownlistFor is returning a null value, how to fix it?

I need some help, on my view i am returning a null value. What i want to get is the list of value when creating dropdownlistFor. The problem is how do i resolve such error? Below is my logic and need some help as to how please team.

    // Model
      // This is for course-list.
  

       // This is for course-list.
        public class eNtsaDashboardViewModel
        {
            public string CourseName { get; set; }
    
            public List<eNtsaDashboardViewModel> CourseLicence { get; set; }
    
            public string Text { get; set; }
            public string Value { get; set; }
    
        }
    
// Mutliple Model
  public class RegCoursesViewModel
    {
        public eNtsaCourses Courses { get; set; }
        public eNtsaDashboardViewModel Dashboard { get; set; }
        public List<eNtsaDashboardViewModel> lsteNtsaDashboard { get; set; }
        public RegCoursesViewModel MainModel { get; set; }

    }
    //  View
                         @using (Html.BeginForm("SuperAdmin", "Home", FormMethod.Post))
                                            {
                                            <div class="form-group row">
                                                <label for="Content-Licence" class="col-sm-3 col-form-label">Content Licence</label>
                                                <div class="col-sm-5">
                                                @Html.DropDownListFor(model => model.Dashboard.CourseLicence, new SelectList(Model.lsteNtsaDashboard)) **// Return a null object.**
                                                </div>
                                            </div>
                                            }
    public ActionResult SuperAdmin() { 
    
    var list = new List<string>() { "Private(Copyrighted)", "Public Domain", "Creative Commons Licences", "CC Attribution", "CC Attribution Share ALike", "CC Attribution Non-Commercial", 
                    "CC Attribution Non-Commercial Share Alike", "CC Attribution Non Directive", "CC Attribution Non-Commercial No Directives" };
                ViewBag.list = list;
    return View();
    }

Upvotes: 0

Views: 330

Answers (2)

Abdullah Mansoor
Abdullah Mansoor

Reputation: 67

I think you should go through the Viewbag concept little mnre. Here you are assigning the list value to the Viewbag and not using that value in the view. In your case, I have done some modifications to your code. In your controller,

public ActionResult SuperAdmin() {
      ViewBag.Message = "Your application description page.";

      List<SelectListItem> list = new List<SelectListItem>()
      {
        new SelectListItem { Text = "Private(Copyrighted)", Value = "1" },
        new SelectListItem { Text = "Public Domain", Value = "2" },
        new SelectListItem { Text = "CC Attribution Non Directive", Value = "3" },
        new SelectListItem { Text = "Creative Commons Licences", Value = "4" },

      };
      ViewBag.list = list;
      return View();
}

Then change your View's dropdown to accept the Viewbag.list value. In your View,

@using (Html.BeginForm("SuperAdmin", "Home", FormMethod.Post))
   {
        <div class="form-group row">
        <label for="Content-Licence" class="col-sm-3 col-form-label">Content Licence</label>
        <div class="col-sm-5">
         @Html.DropDownListFor(model => model.Dashboard.CourseLicence, (IEnumerable<SelectListItem>)ViewBag.list)
         </div>
        </div>
   }

This is not a preferred way of doing the dropdown list, I just modified your code to be working.

Upvotes: 0

Wowo Ot
Wowo Ot

Reputation: 1529

I would recommend that you do it like this

var list = new List<string>() { "Private(Copyrighted)", "Public Domain", "Creative Commons Licences", "CC Attribution", "CC Attribution Share ALike", "CC Attribution Non-Commercial", 
            "CC Attribution Non-Commercial Share Alike", "CC Attribution Non Directive", "CC Attribution Non-Commercial No Directives" };

List<SelectListItem> select_list = new List<SelectListItem>();

var query = from li in list
    select new SelectListItem()
    {
          Value = li.ToString(),
        Text = li.ToString(),
    };
 select_list = query.ToList();
 ViewBag.list = select_list;

then in your view

@Html.DropDownListFor(model => model.Dashboard.CourseLicence, ViewBag.list as List<SelectListItem>)
                                        

Upvotes: 1

Related Questions