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" })
Related
I have a linq query like below:
public IQueryable<vmEmp> GetEmp(int crewid)
{
var dt = new EmpEntities();
var e = from t in dt.tblEmp
where (t.CrewId == crewid)
select new vmEmp
{
Id = -1,
Crew = t.crewid,
Name = t.Name,
Address = t.Address
};
return e;
}
I hope can make the Id auto decrease by 1 till end of the employee.
like first's Id is -1, second's is -2, third is -3 ...
How to do that here? Thanks a lot
If this was LINQ-to-objects, you could use this overload of Select:
var dt = new EmpEntities();
var e = dt.tblEmp
.Where(t => t.CrewId == crewid)
.Select((t,index) => new vmEmp
{
Id = -index - 1,
Crew = t.crewid,
Name = t.Name,
Address = t.Address
});
But EF doesn't suport this, because the indexer can't be translated into SQL. So you need a work-around:
var dt = new EmpEntities();
var e = dt.tblEmp
.Where(t => t.CrewId == crewid)
.Select(t => new
{
Id = 0,
Crew = t.crewid,
Name = t.Name,
Address = t.Address
}
.AsEnumerable() // Continue in memory
.Select((t,index) => new vmEmp
{
Id = -index - 1,
Crew = t.Crew,
Name = t.Name,
Address = t.Address
});
Side note: it's recommended to put dt in a using construct.
Use a counter variable and decrease it for every record while you project it to your custom POCO.
public IQueryable<vmEmp> GetEmp(int crewid)
{
int counter=0;
var dt = new EmpEntities();
//load the items to a list of anonymous type
var eList = from t in dt.tblEmp
where (t.CrewId == crewid)
.Select(s=>
new { Id = 0,
Crew = s.crewid,
Name = s.Name,
Address = s.Address
}).ToList();
var e=eList.Select(x=> new vmEmp
{
Id = --counter,
Crew = x.Crew,
Name = x.Name,
Address = x.Address
});
return e.AsQueryable();
}
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
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" })
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);
}
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");