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 >
@Html.LabelFor(model => model.IsDegreeCompleted, htmlAttributes: new { @class = "control-label " })
<div >
@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.MvcStackTrace:
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(IList
1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
CodePudding user response:
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 })