Set Default Date with DropdownListFor using Enumerable Range - asp.net

i'm trying set into dropdown's current date i'm tried many variants how to achieve this.. so here is my sample code..
#{ var days = Enumerable.Range(1, 31).
Select(i => new SelectListItem {Value = i.ToString(), Text = i.ToString(), Selected = i == DateTime.Now.Day}).ToList();
var month = Enumerable.Range(1, 12).
Select(i => new SelectListItem {Value = i.ToString(), Text = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(i), Selected = i == DateTime.Now.Month}).ToList();
var years = Enumerable.Range(2000, DateTime.Now.Year - 2000 + 4).
Select(i => new SelectListItem {Value = i.ToString(), Text = i.ToString(), Selected = i == DateTime.Now.Year}).ToList();
}
#Html.DropDownListFor(model => model.SDay, days)
#Html.DropDownListFor(model => model.SMonth, month)
#Html.DropDownListFor(model => model.SYear, years)
as you can see i set Today in Selected option for day, month and year, debugger show me that today day is selected Debugger but on view day is set at 1

Related

Viewbag New Selectlist Placeholder

I have a simple select list being generated in my action method (code below)
Viewbag.Select = new SelectList(db.esp_admin_form_demographics.Where(x => x.category == (int) SelectHelper.ClientSelectOptions.Demographics.BirthCountry || x.category == 999), "id", "value", "");
How is it possible to add a placeholder value in there as the default value?
Here is the solution to set placeholder as the default value in your SelectList/DropDownList :
In the Controller Method:
var espAdminList = db.esp_admin_form_demographics.Where(x => x.category == (int) SelectHelper.ClientSelectOptions.Demographics.BirthCountry || x.category == 999)
Viewbag.EspAdminSelectList = new SelectList(espAdminList, "id", "value");
Then in the view:
#Html.DropDownList("EspAdminId",ViewBag.EspAdminSelectList as SelectList, "Select Esp. Admin", new { #class = "form-control" })

How to assign key and count to two different labels asp.net

I have this query:
var months = dates.GroupBy(
x => x.Value.Month).Select(g => new { Month = g.Key, Count = g.Count()});
lbl1.Text = string.Join(",", months);
How do I assign key and count to two different labels?
Try this
lblMonth.Text=months.Month;
lblCount.Text=months.Count;
Also you have to call FirstOrDefault() or ToList() in order to select data. Currently you code will not select data.
var months = dates.GroupBy(x => x.Value.Month).Select(g => new { Month = g.Key, Count = g.Count() }).FirstOrDefault();
OR
var months = dates.GroupBy(x => x.Value.Month).Select(g => new { Month = g.Key, Count = g.Count() }).ToList();
if you use ToList() than you will have to get values by index like
lblMonth.Text=months[0].Month;
lblCount.Text=months[0].Count;

How do I give my dropdownlist an id?

Hi I tried examples on the site but can't find solution. I get no error but the id is not assigned to my dropdown list. How do I assign an id to this dropdown list?
#Html.DropDownList("searchString", Model.WeekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString(), Selected = s.ToString().Equals("Day") }), new { #id = "listId" })
Thanks for any help!
#Html.DropDownList("searchString", Model.WeekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString(), Selected = s.ToString().Equals("Day") }))
The first string argument in #Html.DropDownList is its id, remove the id u are giving as attribute.
try below code :-
#Html.DropDownList("searchString", Model.WeekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString(), Selected = s.ToString().Equals("Day") }), new { id = "listId" })

Html.DropDownListFor value not selected on edit mode

I am succesfullly able to save value to database (title value) on insert , but when i render the same view in edit mode then title field must hold the selected value but in my case no value is selected by title dropdown...dont know why i am getting a dropdown with nothing selected while title field holds the stored value(at backend).
#Html.DropDownListFor(model => model.title, new SelectList(Model.titles, "Value", "Text"),"-Select-") // nothing selected on edit mode
#Model.title //displaying the stored value which the user selected initially.
values for title
titles = new SelectList(ListItem.getValues().ToList(), "Value", "Text").ToList();
getValue function
public static List<TextValue> getValues()
{
List<TextValue> titles= new List<TextValue>();
TextValue T= new TextValue();
T.Value = "Mr";
T.Text = "Mr";
titles.Add(T);
T= new TextValue();
T.Value = "Mrs";
T.Text ="Mrs";
titles.Add(T);
T= new TextValue();
T.Value = "Miss";
T.Text = "Miss";
titles.Add(T);
T= new TextValue();
T.Value ="Other";
T.Text = "Other";
titles.Add(T);
return titles;
}
You've got to use another ctor of SelectList
From msdn
SelectList(IEnumerable, String, String, Object)
Initializes a new instance of the SelectList class by using the
specified items for the list, the data value field, the data text
field, and a selected value.
Then :
#Html.DropDownListFor(model => model.title,
new SelectList(Model.titles, "Value", "Text", Model.title),
"-Select-")
By the way, it's generally a good idea to follow basics standards (at least) : your properties should begin by an Upper case char.
public string Title {get;set;}
Views:
#Html.DropDownListFor(model => model.title, Model.titles, "-Select-")
Controllers:
Model.titles = new SelectList(ListItem.getValues(), "Value", "Text");
public static List<SelectListItem> getValues()
{
List<SelectListItem> titles= new List<SelectListItem>();
SelectListItem T= new SelectListItem();
T.Value = "Mr";
T.Text = "Mr";
titles.Add(T);
T = new SelectListItem();
T.Value = "Mrs";
T.Text = "Mrs";
titles.Add(T);
T = new SelectListItem();
T.Value = "Miss";
T.Text = "Miss";
titles.Add(T);
T = new SelectListItem();
T.Value = "Other";
T.Text = "Other";
titles.Add(T);
return titles;
}
public ActionResult Edit(int sno)
{
var model = db.table.SingleOrDefault(x => x.sno == sno);
return View(model);
}

how to convert the int value into string to show in grid in asp.net mvc

i have tried the following code and it is giving error
var res = (from results in db.JobSearchAgents
where results.SiteID == 110 && results.UserID == sess
select new Agentlist
{
JobSearchAgentID = results.JobSearchAgentID.ToString(),
EmailAddress = results.EmailAddress,
Keywords = results.Keywords,
Country = results.Country,
zipcode = results.ZipCode,
miles = results.Miles.ToString()
}).AsEnumerable();
ViewData["ajax"] = true;
ViewData["scrolling"] = true;
ViewData["paging"] = true;
ViewData["filtering"] = true;
ViewData["grouping"] = true;
ViewData["sorting"] = true;
ViewData["showFooter"] = true;
//ViewData["searchresults"] = res;
return View(res);
<%using (Html.BeginForm())
{%>
<%=Html.Telerik().Grid(Model).Name("Grid").Columns(columns=>
{
columns.Bound(m=>m.Keywords);
columns.Bound(m=>m.Country);
}).DataBinding(databinding=>
{
databinding.Server().Select("Agentlist","Grid",new
{
ajax=ViewData["ajax"]
});
databinding.Ajax().Select("Agentlist",
"Grid").Enabled((bool)ViewData["ajax"]);
})
.Scrollable(scrolling => scrolling.Enabled((bool)ViewData["scrolling"]))
.Sortable(sorting => sorting.Enabled((bool)ViewData["sorting"]))
.Pageable(paging => paging.Enabled((bool)ViewData["paging"]))
.Filterable(filtering => filtering.Enabled((bool)ViewData["filtering"]))
.Groupable(grouping => grouping.Enabled((bool)ViewData["grouping"]))
.Footer((bool)ViewData["showFooter"])
%>
<%}%>
The Error is because your query is trying to execute ToString method in db. using AsEnumerable you can tell the query to execute your methods in c#. you can either change your query to
var res = (from results in db.JobSearchAgents
where results.SiteID == 110 && results.UserID == sess
select result).AsEnumerable().Select(result=>new Agentlist
{
JobSearchAgentID = results.JobSearchAgentID.ToString(),
EmailAddress = results.EmailAddress,
Keywords = results.Keywords,
Country = results.Country,
zipcode = results.ZipCode,
miles = results.Miles.ToString()
});
Or you can bind your columns to telerik grid without calling ToString method. Telerik grid does not require the data to be in string format. Moreover, if you want data to be displayed in specific format you can call Format method when binding to the grid
columns.Bound(m=>m.Keywords).Foramt("format string");

Resources