locked
Bind data in Dropdownlistfor RRS feed

  • Question

  • User615038210 posted

    Hi 

    i want  to bind data in dropdownlist from datavbase but i'm getting this error 

    DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'CallId'. 

    so how can i'm doing this is write way.  any one know.?

    model

    [Key]
            public Guid CallId { get; set; }
    
            [Required(ErrorMessage = "This field is required")]
    
            public string CallTypeName { get; set; }
    method
    public ActionResult AddNewDIDInformation()
            {
    
                ModelContext context = new ModelContext();
                {
                    ViewBag.calltype = context.callType.Select(x => new SelectListItem { Text = x.CallTypeName, Value = x.CallId.ToString() }).ToList();
                    return View();
                }
    
    Html.......
    <div class="form-group">
                @Html.Label("DID CallType", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
    
                    @Html.DropDownListFor(Model=> Model.DID_CallType,new SelectList(ViewBag.calltype, "CallId", "CallTypeName"),
        "Select Call Type",
        new { @class = "form-control" }))
                    @* @Html.EditorFor(model => model.DID_CallType)*@
                    @Html.ValidationMessageFor(model => model.DID_CallType)
                </div>
            </div>


    Tuesday, December 20, 2016 5:42 AM

Answers

  • User177399542 posted

    First of all read my name carefully.

    Have you tried this?

    @Html.DropDownListFor(Model=> Model.DID_CallType,new SelectList(ViewBag.calltype, "Value", "Text"),


    This will also not work:

    x.CallId.ToString() }).ToList();
    
    //.ToString() will also not work. To resolve this issue do this:
    
    1) Add name space: 
    using System.Data.Objects.SqlClient;
    
    2) Change your code to:
    ViewBag.calltype = context.callType.Select(x => new SelectListItem { Text = x.CallTypeName, Value = SqlFunctions.StringConvert(x.CallId) }).ToList();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2016 6:52 AM
  • User-1768369891 posted

    You mention wrong dropdown list binding expression 

     @Html.DropDownListFor(Model=> Model.DID_CallType,new SelectList(ViewBag.calltype, "Value", "Text"),
        "Select Call Type",
        new { @class = "form-control" }))
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2016 6:57 AM
  • User177399542 posted

    Do like this:

    var callTypeList = context.callType.ToList();
    ViewBag.calltype = callTypeList.Select(x => new SelectListItem { Text = x.CallTypeName, Value = x.CallId.ToString() }).ToList();
    
    

    Then:

    @Html.DropDownListFor(Model=> Model.DID_CallType,new SelectList(ViewBag.calltype, "Value", "Text")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2016 7:40 AM

All replies

  • User177399542 posted

    Hi You are converting your CallTypeName to Text and CallId to Value

    ViewBag.calltype = context.callType.Select(x => new SelectListItem { Text = x.CallTypeName, Value = x.CallId.ToString() }).ToList();

    So there is need to modify your html code:

    @Html.DropDownListFor(Model=> Model.DID_CallType,new SelectList(ViewBag.calltype, "Value", "Text"),

    Tuesday, December 20, 2016 6:40 AM
  • User615038210 posted

    Hi Anju 

    Actually i have CallId Guid so i'm convert into Tostring()

    ViewBag.calltype = context.callType.Select(x => new SelectListItem { Value = x.CallId.ToString(), Text = x.CallTypeName }).ToList();


      i'm tired to doing this i'm spent 2.5 hour on this minor error 

    Tuesday, December 20, 2016 6:47 AM
  • User177399542 posted

    First of all read my name carefully.

    Have you tried this?

    @Html.DropDownListFor(Model=> Model.DID_CallType,new SelectList(ViewBag.calltype, "Value", "Text"),


    This will also not work:

    x.CallId.ToString() }).ToList();
    
    //.ToString() will also not work. To resolve this issue do this:
    
    1) Add name space: 
    using System.Data.Objects.SqlClient;
    
    2) Change your code to:
    ViewBag.calltype = context.callType.Select(x => new SelectListItem { Text = x.CallTypeName, Value = SqlFunctions.StringConvert(x.CallId) }).ToList();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2016 6:52 AM
  • User-1768369891 posted

    You mention wrong dropdown list binding expression 

     @Html.DropDownListFor(Model=> Model.DID_CallType,new SelectList(ViewBag.calltype, "Value", "Text"),
        "Select Call Type",
        new { @class = "form-control" }))
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2016 6:57 AM
  • User615038210 posted

    Give error message

    Severity Code Description Project File Line Suppression State
    Error CS1503 Argument 1: cannot convert from 'System.Guid' to 'double?'

    Tuesday, December 20, 2016 7:16 AM
  • User177399542 posted

    Do like this:

    var callTypeList = context.callType.ToList();
    ViewBag.calltype = callTypeList.Select(x => new SelectListItem { Text = x.CallTypeName, Value = x.CallId.ToString() }).ToList();
    
    

    Then:

    @Html.DropDownListFor(Model=> Model.DID_CallType,new SelectList(ViewBag.calltype, "Value", "Text")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2016 7:40 AM
  • User615038210 posted

    ok thanks 

    Tuesday, December 20, 2016 7:53 AM
  • User615038210 posted

    When i'm post data then display this error 

    please review this screenshot 

    http://my.jetscreenshot.com/29458/20161220-k3bp-44kb.png

    Tuesday, December 20, 2016 8:17 AM
  • User-1768369891 posted

    Try this

    @Html.DropDownListFor(Model=> Model.DID_CallType,new  SelectList((IEnumerable<SelectListItem>)ViewBag.calltype, "Value", "Text"),
        "Select Call Type",
        new { @class = "form-control" }))

    Tuesday, December 20, 2016 8:36 AM
  • User615038210 posted

    my all method are execute properly but when post data then error occur on dropdownlist in html

    http://my.jetscreenshot.com/29458/20161220-od00-116kb.png 

    Tuesday, December 20, 2016 9:00 AM
  • User615038210 posted

    same error occur again............

    Value cannot be null.
    Parameter name: items

    Tuesday, December 20, 2016 9:01 AM