locked
Dropdown list is not sending value "Y" or "N" why? RRS feed

  • Question

  • User-199788946 posted
    public IEnumerable<SelectListItem> GetYesNoDropDown(object selectedValue)
    {
    return new List<SelectListItem>
    {
    new SelectListItem{ Text="Yes", Value = "Y", Selected = "Y" == selectedValue.ToString()},
    new SelectListItem{ Text="No", Value = "N", Selected = "N" == selectedValue.ToString()},
    };
    }
    
    var list = GetYesNoDropDown(user.IsActive); 
    ViewData["yesnoList"] = list;
    
    @Html.DropDownList("yesnoDropDown", ViewData["yesnoList"] as List<SelectListItem>, new { @class = "form-control" })

    when I do 

    I have model property IsActive How can I point to my model property from DropDownList,

    when I do 

    @Html.DropDownList(model=>model.IsActive,"yesnoDropDown", ViewData["yesnoList"] as List<SelectListItem>, new { @class = "form-control" })

    intellisense is not showing my property IsActive and it is giving me error: 

    Error CS1660 Cannot convert lambda expression to type 'string' because it is not a delegate type why?
    I want to send selected property value IsActive to the server.

    Thursday, May 21, 2020 1:35 PM

Answers

  • User-474980206 posted

    as I said, DropDownList does not take a lambda (delegate), it want the string name of the model property. if the property is IsActive, then its:

    @Html.DropDownList("IsActive", ViewData["yesnoList"] as List<SelectListItem>, new { @class = "form-control" })

    if you want intellisense, then use the DropDownListFor which was designed for intelligence (it call the above);

    @Html.DropDownListFor(m=>m.IsActive, ViewData["yesnoList"] as List<SelectListItem>, new { @class = "form-control" })
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 3:16 PM

All replies

  • User-474980206 posted

    Only the DropdownListFor accepts a delegate. Try

    @Html.DropDownList(“IsActive”, ViewData["yesnoList"] as List<SelectListItem>, new { @class = "form-control" })
    Thursday, May 21, 2020 2:24 PM
  • User-199788946 posted

    Hello bruce,

    IsActive is property in @model NicUserManagement.Models.ViewModel.User_TableVm it is not binding to model=>model.IsActive property. It is not even showing by IDE IntelliSense . I want to bind that property but in current context it is not doing that 

            public IEnumerable<SelectListItem> GetYesNoDropDown(object selectedValue)
    {
    return new List<SelectListItem>
    {
    new SelectListItem{ Text="Yes", Value = "Y", Selected = "Y" == selectedValue.ToString()},
    new SelectListItem{ Text="No", Value = "N", Selected = "N" == selectedValue.ToString()},
    };
    }
    void ActionResult Edit(int id){
    var user = DBEntities.User_Table.FirstOrDefault(x => x.Id == Id);
    var list = GetYesNoDropDown(user.IsActive);
    ViewData["yesnoList"] = list;
    }
    [HttpPost]
    void AcitionResult Edit(User_TableVm uv){

    }

    @Html.DropDownList("yesnoDropDown", ViewData["yesnoList"] as List<SelectListItem>, new { @class = "form-control" })

    when i write above code it is showing me dropdown when I press send button I want to bind property to the model. But problem is I am not able to bind property from my model

    When I bind model to by viewmodel it gives me error also I can't able to receive value from GetYesNoDropDown method .

    When i Press send button GetYesNoDropDown method Value property to IsActive property.



    @Html.DropDownList(model=>model.IsActive,"yesnoDropDown", ViewData["yesnoList"] as List<SelectListItem>, new { @class = "form-control" })
    Thursday, May 21, 2020 2:51 PM
  • User-474980206 posted

    as I said, DropDownList does not take a lambda (delegate), it want the string name of the model property. if the property is IsActive, then its:

    @Html.DropDownList("IsActive", ViewData["yesnoList"] as List<SelectListItem>, new { @class = "form-control" })

    if you want intellisense, then use the DropDownListFor which was designed for intelligence (it call the above);

    @Html.DropDownListFor(m=>m.IsActive, ViewData["yesnoList"] as List<SelectListItem>, new { @class = "form-control" })
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 3:16 PM
  • User-199788946 posted

    Thanks bruce for your time.

    Thursday, May 21, 2020 3:23 PM