I have this line in my view.aspx I am not using razor:
<p style="color:Red">
<% Html.DropDownListFor(model => model.AllSundays, new SelectList(Model.AllSundays)); %>
</p>
When I run my program and view source there is nothing there.
I am making use of the ViewModel in my action I have:
var allSundaysInThisMonth = new SundaysInMonthViewModel
{
AllSundays = sundays.Select(x => new SelectListItem
{
Value = x.ToString("dd-MM-yyyy"),
Text = x.ToString("dd-MM-yyyy"),
})
};
var selectedSunday = new SundaysInMonthViewModel{
SelectedSunday = thisMonthSundays.Where(x => x < now)
.Last().ToString("dd-MM-yyyy")
};
return View(allSundaysInThisMonth);
thanks
You should use
<%=and no comma at the end if you want to output something to the response, otherwise you are only executing server side code that never gets flushed to the response:Also
AllSundaysseems to already be anIEnumerable<SelectListItem>so you don’t need to feed that to aSelectListin the view:Also (and this is extremely important), you should not use the same property as both the selected value and the available values. Currently you are using
AllSundaystwice. That’s absolutely wrong. The DropDownList helper expects as first argument a scalar property on your view model to hold the selected value and anIEnumerable<SelectListItem>property as second argument that will hold the selected values.So what you want is: