locked
How to Prepend Value to SelectList RRS feed

  • Question

  • User657329123 posted

    Hi there,

    In my controller, I have following

    ViewBag.p1 = new SelectList(db.M1.OrderBy(x => x.s1), "x", "x");

    in my view, I have following

    if (String.IsNullOrEmpty(Model.M[i].x))
       {
           @Html.DropDownListFor(m => m.M[i].x, (SelectList)ViewBag.p1, "-- Please Select x --", new { @class = "form-control" })
        }
        else
            {
             @Html.DropDownListFor(m => m.M[i].x, (SelectList)ViewBag.P1, Model.M[i].x, new { @class = "form-control" })
        }
    

    If the Model.M[i].x is null or empty, I see "-- Please Select x --" wtih value "" but if the value of Model.M[i].x is not NULL or empty then dropdown list shows me the selected value in the Edit view. But I need to add to dropdown list an empty value in case user changes the mind. E.g. If the Edit view shows value "Bag" selected. I want to give user option to remove "Bag" by selecting option "-- Please Select x --".

    Any idea how to do this?

    Joe

    Monday, December 24, 2018 4:09 PM

All replies

  • User1724605321 posted

    Hi joegreen2005 ,

    Just use :

    @Html.DropDownListFor(m => m.M[i].x, (SelectList)ViewBag.p1, "-- Please Select x --", new { @class = "form-control" })

    If m.M[i].x is empty  , it will show "-- Please Select x --" , if it has value , it will shows the value which matches the selectList item .

    Best Regards,

    Nan Yu

    Tuesday, December 25, 2018 8:33 AM
  • User657329123 posted

    If If m.M[i].x is not empty , it replaces "-- Please Select x --" , with the value which matches the selectList item. Then user doesn't have a way to set the value to empty or null  because "-- Please Select x --" doesn't appear in the select list anymore. In Edit view, I need to provide user a way to set the value to NULL or empty.

    Tuesday, December 25, 2018 2:58 PM
  • User1724605321 posted

    Hi joegreen2005,

    If If m.M[i].x is not empty , it replaces "-- Please Select x --" , with the value which matches the selectList item. Then user doesn't have a way to set the value to empty or null  because "-- Please Select x --" doesn't appear in the select list anymore

     Use `@Html.DropDownListFor(m => m.M[i].x, (SelectList)ViewBag.p1, "-- Please Select x --", new { @class = "form-control" })` , when m.M[i].x is not empty , the defalut value is the matched item ,but he still be able to choose `-- Please Select x --` since you have insert "- Please Select x --" to dropdownlist .

    Best Regards,

    Nan Yu

    Wednesday, December 26, 2018 6:08 AM
  • User657329123 posted

    Sorry Nan Yu but it's not working as you say.  If m.M[i].x is not empty , it replaces "-- Please Select x --" , with the selected value thereby removing "--Please Select x--" with value empty string.  Is this how select List behaves in .NET? This is really wired behavior, hopefully someone can explain why is it happening?

    For the time being I'm adding "--Please Select x--" with value empty string using jQuery if Select List doesn't contain option with text "--Please Select x--" so that user can set it default value.

    Friday, December 28, 2018 2:10 PM
  • User1724605321 posted

    Hi joegreen2005 ,

    If m.M[i].x is not empty , it replaces "-- Please Select x --" , with the selected value thereby removing "--Please Select x--" with value empty string.  Is this how select List behaves in .NE

    I tried that it works on my side , it will show "-- Please Select x --" as one opinion in my select . Could you please provide any more details to help reproduce ? 

    Best Regards,

    Nan Yu

    Monday, December 31, 2018 8:06 AM
  • User657329123 posted

    Here is my model. I'm having a problem with dropdown list in nested list.

    public class Test1{
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public Guid Id { get; set; }
    	public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Population { get; set; }
            public List<Something> M { get; set; }
    }
    
    public class Something {
    	public int Id { get; set; }
    	public string x { get; set; }
    }

    In Edit view, I have following,

    @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
    @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
    @Html.DropDownListFor(model => model.Population, (SelectList)ViewBag.A, "Please Select", new { @class = "form-control" })
    @if (Model.M != null)
    {
    	if (Model.M.Count() > 0)
    	{
    		for (int i = 0; i < Model.M.Count(); i++)
    		{
    			@Html.DropDownListFor(m => m.M[i].x, (SelectList)ViewBag.P1, "--Please select--" , new { @class = "form-control" })
    		}
    	}
    }

    When I select Population, the dropdownlist is behaving correctly as you said and see in the screenshot.

    popddl

    I'm having problem with dropdown list x that is part of Something list in Edit view. In Edit view I see this

    xddl

    What I cannot understand is why 15 selected value appears at the beginning of the list (highlighted). It doesn't happen in a dropdown list Population. Any ideas? I haven't been able to find any code with a nested dropdown list anywhere. It's driving me crazy.

    Monday, December 31, 2018 7:02 PM