0

I know there are many answers to this type of question, I have read them all and still cannot work this out.

I have a dropdown I am populating in the controller using a SelectList and transferring using a viewbag. I have simplified it in the following, just 0,1 & 2, with the same value for Text & Value.

I really want text of 'None', 'Weekly' and 'Monthly', and tried viewmodel values of 1 and Weekly, neither gets displayed.

The viewmodel is currently passing an initial value of 1 (I have tried passing both string and int values, and also checked it is passing 1 using EditorFor rather than DropDownFor), but the dropdown displays the first value 0, not the model value.

I have got this working elsewhere, but the SelectList is populated from a db table as follows. In this case the model passes a value for ID and the correct initial value for Aircraft is displayed.

var nAir = (from n in db.Handicaps
    orderby n.Aircraft
    where n.ClubsID == ClubsID
    select n).ToList();

ViewBag.Aircraft = new SelectList(nAir, "ID", "Aircraft");

But when I populate the list manually, the model value is ignored.

I am obviously missing something, but cannot work out what it is. I could set up a table for EmailFreq and do it like the example above but there are only 3 values in the dropdown so it seems overkill.

Controller :

ViewBag.EmailFreq = new SelectList(new List<SelectListItem>
{
    new SelectListItem { Value = "0", Text = "0"},
    new SelectListItem { Value = "1", Text = "1" },
    new SelectListItem { Value = "2", Text = "2" }
}, "Value", "Text");    

var profile = new ClubProfileEdit
{
    ClubID = ClubsID,
    ClubName = Session["ClubName"].ToString(),        
    EmailFreq = existProfile.EmailFreq.ToString()
};

return View(profile);

The view:

<div class="form-group">
    @Html.LabelFor(model => model.EmailFreq, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.EmailFreq, new SelectList(ViewBag.EmailFreq, "Value", "Text"), new { @class = "form-control" } )
        @Html.ValidationMessageFor(model => model.EmailFreq, "", new { @class = "text-danger" })
    </div>
</div>

The Viewmodel:

namespace OAcontest.ViewModels
{
public class ClubProfileEdit
{
    public int ID { get; set; }

    public int ClubID { get; set; }

    public string ClubName { get; set; }

    public string EmailFreq { get; set; }
}
}
Brian.S
  • 131
  • 12
  • Do you get an error...? Is it the second part (with ClubProfile) you're trying to get working? – 321X May 16 '17 at 14:27
  • That doesn't quite right. You're creating a SelectList in your controller, but in your view you're treating it as if it's a List and trying to turn it into a SelectList again – John M May 16 '17 at 15:11
  • In addition to the dupe, it is pointless to use `new SelectList(...)` in your view to create another identical `IEnumerable` from the original one. –  May 16 '17 at 21:34
  • Thanks to @Stephen Muecke. Changing the ViewBag name solved the issue. Would have saved a day had I known that. Also changed helper to >>, (IEnumerable)ViewBag.EFreq,<< to solve dup issue, is that correct? – Brian.S May 16 '17 at 22:28
  • You could do `ViewBag.EFreq as SelectList`. You can prevent this by adding the list to your ViewModel, strongly typed. – 321X May 17 '17 at 08:09

0 Answers0