1

I try to create dropdown list. But it give me error. First dropdown is working which I try with simple list. But in second dropdown which I create with Value and text It give me error.

I adopted code first approach and created bool type property in model class. I want Yes or No form user through dropdown list and store its corresponding value in database for yes value is 1 and for No value is 0. So I try SelectListItem to pass the value and text to View. But its not working:

Here is my model class:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Edu_Form.Models
{
    public class Hiring_Edu_Info
    {
        [Key]
        public int Id { get; set; }
        public String Level { get; set; }
        public bool IsDegreeCompleted { get; set; }
    }
} 

Here is my controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Edu_Form.Controllers
{
    public class HiringController : Controller
    {
        // GET: Hiring
        public ActionResult Index()
        {
            List<SelectListItem> DegreeCompleted = new List<SelectListItem>() {
                      new SelectListItem {
                          Text = "Yes", Value = "1"
                      },
                      new SelectListItem {
                          Text = "No", Value = "0"
                      },
                };

            ViewBag.LevelList = DegreeCompleted;
            return View();
        }
}

Here is my view:

@model Edu_Form.Models.VMHiring_edu_Info

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
<div class="form-group">
                    @Html.LabelFor(model => model.IsDegreeCompleted, htmlAttributes: new { @class = "control-label " })
                    <div class="">

                        @Html.DropDownListFor(model => model.IsDegreeCompleted, new SelectList(ViewBag.DegreeCompleted, "Value", "Text"), "-- Select --", new { @class = "form-control" })
                        @* @Html.EditorFor(model => model.IsDegreeCompleted)*@
                        @Html.ValidationMessageFor(model => model.IsDegreeCompleted, "", new { @class = "text-danger" })

                    </div>
                </div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

This line throws an error:

@Html.DropDownListFor(model => model.IsDegreeCompleted, new SelectList(ViewBag.DegreeCompleted, "Value", "Text"), "-- Select --", new { @class = "form-control" })

Error details:

System.ArgumentNullException
HResult=0x80004003
Message=Value cannot be null.
Parameter name: items
Source=System.Web.Mvc

StackTrace:

at System.Web.Mvc.MultiSelectList..ctor(IEnumerable items, String dataValueField, String dataTextField, String dataGroupField, IEnumerable selectedValues, IEnumerable disabledValues, IEnumerable disabledGroups)
at ASP._Page_Views_NUML_Hiring_Index_cshtml.Execute() in C:\Users\Shaheer_Ahmed\Desktop\HRM_NUML\Edu_Form\Edu_Form\Views\NUML_Hiring\Index.cshtml:line 71
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.StartPage.RunPage()
at System.Web.WebPages.StartPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

enter image description here

2
  • 1
    In the controller, you are adding dynamic property LevelList to ViewBag, but in the view, you are accessing it incorrectly as ViewBag.DegreeCompleted. The correct way is to access the data using the dynamic property, like, ViewBag.LevelList. Commented Sep 11, 2022 at 19:19
  • But it give me String type value and in my database it type is bit. So how to convert String value to bit? Commented Sep 11, 2022 at 19:53

1 Answer 1

1

Build the SelectList under your controller and pass to the view through viewbag

 var OType = new List<SelectListItem>();
                OType.Add(new SelectListItem() { Text = "Yes", Value = "0" });
                OType.Add(new SelectListItem() { Text = "No", Value = "1" });
    
                SelectList DegreeCompleted = new SelectList(OType, "Value", "Text");
    
    ViewBag.DegreeCompleted = DegreeCompleted ;

Under the view, you can do something like this.

@Html.DropDownListFor(model => model.IsDegreeCompleted, (SelectList)ViewBag.DegreeCompleted, "-- Select --", new { @class = "form-control" })

If you want to add value to the DropDownList, you can do like

 @Html.DropDownListFor(model => model.IsDegreeCompleted, (SelectList)ViewBag.DegreeCompleted, "-- Select --", new { @class = "form-control", @Value = @item.value })
Sign up to request clarification or add additional context in comments.

4 Comments

In data base its column data type is bit and value is in String type so how to convert value from String to bit?
Actually it is 0 and 1 anyway. For your case, it will work even with this.
But I want to convert it with true for 1 and false for 0 and then store in data base. I have try it when I pass view data to my model class property in controller but it give me 1 and 0 IsDegreeCompleted = Tb_Edu.IsDegreeCompleted == "1" ? true : false,
Maybe u could publish that part of ur code in another question. I take a look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.