I have this part of code that runs very well:
merge1.Statuses = db.JobStatus.Select(s => new SelectListItem
{
Value = s.JobStatusID.ToString(),
Text = s.JobStatusName
}).ToList();
Now, I want to add a default value that is a dynamic value, depending on the database. So, I tried something like:
private SelectListItem statuses(string defaultStatus)
{
db.JobStatus.Select(s => new SelectListItem
{
Value = s.JobStatusID.ToString(),
Text = s.JobStatusName,
defaultStatus
}).ToList();
return ???
}
So, I have marked red on defaultStatus.
Thanks in advance
I would do it like this
private IEnumerable<SelectListItem> statuses(string defaultStatus)
{
return db.JobStatus.Select(s => new SelectListItem
{
Value = s.JobStatusID.ToString(),
Text = s.JobStatusName,
Selected = s.JobStatusName == defaultStatus
}).ToList();
}
Also change the return type so you can bind it to a select list in the view.
Related
I am trying to bind the dropdown list to the data-set coming from the data context class in mvc 6. I wrote a function to get the populated list but unable to reproduce the same using razor. Here's what I have so far. Please note that I have not created a model yet. trying to make use of the generated POCO class from the database scaffolding.
function on Layout.cshtml
#functions{
public List<HSIP.Entities.StateDetails> function1()
{
// protected readonly HSIP.Entities.HSIPContext context;
HSIP.Entities.HSIPContext hsipcontext = new HSIP.Entities.HSIPContext();
List<HSIP.Entities.StateDetails> getstatelist = (from s in hsipcontext.StateDetails
select new HSIP.Entities.StateDetails
{
StateDesc = s.StateDesc,
StateCode = s.StateCode,
StateAbbr = s.StateAbbr
}).ToList();
//SelectList list = new SelectList(getstatelist, "Region", "StateCode", "StateAbbr", "StateDesc");
return getstatelist;
}
}
Razor syntax:
#Html.DropDownList("StateDesc", #function1(), "Please select State Name");
The Razor syntax throws an error: there is no argument given that corresponds to the required formal parameter 'htmlattributes' of IHTMLHelper.Dropdownlist(string, IEnumerable, string, object).
can someone please point me in the right direction.
Thanks,
Hari
I am prefer do this:
In a controller/Model:
using System.Web.Mvc;
public List<SelectListItem> DropdownListFilter()
{
var listitem = new List<SelectListItem>();
listitem.Add(new SelectListItem { Text = "Dropdown1", Value = "0", Selected = true });
listitem.Add(new SelectListItem { Text = "Dropdown2", Value = "1", Selected = false });
listitem.Add(new SelectListItem { Text = "Dropdown3", Value = "2", Selected = false });
return listitem;
}
When I Load in the ActionResult Just add this following Line:
ViewBag.FilterDropdown = ar.DropdownListFilter().ToList();
And in the view you have to call Filter dropdown like this:
#Html.DropDownList("FilterDropdown")
Hope this help.
Firstly use a SelectListItem in your controller and pass it to your view.Then use it in Razor syntax to populate the dropdown.
List<SelectListItem> stateList = (from s in hsipcontext.StateDetails
select new HSIP.Entities.StateDetails
{
StateDesc = s.StateDesc,
StateCode = s.StateCode,
StateAbbr = s.StateAbbr
}).ToList();
View:
#Html.DropDownListFor("StateDesc", stateList ,"Please select State Name")
I need to create a dropdown list and I'm doing this by creating a new SelectList based on a model. Here is how I am trying to do this:
#Html.DropDownListFor(model=>model.SelectedStudio, new SelectList(Model.Studios, "Id", "Name", Model.Studio)
I see from the class signature that the last parameter is object selectedValue. I have now tried many variations, trying to pass a string there, an object, etc. but when I compile the program, the selected value for the dropdown list is never set to what I want.
Can anyone explain how this works?
EDIT: I don't know if this is important, but the Id of Studio is of type ObjectId (from the MongoDB C# Driver)
I think your not setting for the selected value on new SelectList
your code should be
#Html.DropDownListFor(model=>model.SelectedStudio, new SelectList(Model.Studios, "Id", "Name", [Defualt value])
the reason it not give the error becuase it is object. and your setting up your IEnumerable list. so it is object. see the following link
http://msdn.microsoft.com/en-us/library/dd492553(v=vs.118).aspx
Try this:
#Html.DropDownListFor(model=>model.SelectedStudio, new SelectList(Model.Studios, "Id", "Name", Model.Studio.Id)
this is how u keep a value selected in drop down
#Html.DropDownList("statussearch", new List<SelectListItem>
{
new SelectListItem { Text = "text1", Value = "1", Selected = Studio.IsSelected.HasValue ? Studio.IsSelected.Value : false},
new SelectListItem { Text = "text2", Value = "2"},
new SelectListItem { Text = "text3", Value = "3"},
}, "Select Status")
IsSelected is a field in your DB wch states wch value to be selected.
this will work fine for your requirement
#foreach (var value in studios)
{ #Html.DropDownList("statussearch", new List<SelectListItem>
{new SelectListItem { Text =value.prop1, Value = "1" },new SelectListItem { Text = value.prop2, Value = "2"},new SelectListItem { Text = value.prop3, Value = "3"},}, value.proptobeselected)}
I am still reasonably new to C# and trying to understand list objects.
I have a list object with key/value pairs in it, populated from a database (that part is working). I then want to select a value from that list, using the 'key'. From what I have read I think Linq might be the best way to do this, however I cannot quite work out the syntax I need.
I have a list object as follows:
// Create a list of Items from the database, where Value is the 'key' and Text is the 'value'
IEnumerable<SelectListItem> ItemList = dbLists.ItemList.Select(x => new SelectListItem { Text = x.Description, Value = x.Id.ToString() });
I then want to populate another variable with the description of the selected item in the model, where the model only saves the id.
I have tried various linq Where and Select queries, but cannot work it out.
The easiest way I can think to explain what I am trying to achieve is to use sql syntax,
string SelectedItemDescription = SELECT Text FROM ItemList WHERE ItemList.Value = model.ItemCode
An example of a scenario would be something like:
ItemList
{
(Value = "1", Text = "Item 1"),
(Value = "2", Text = "Item 2"),
(Value = "3", Text = "Item 3"),
...
};
model.ItemCode = 2;
// How do I make:
SelectedItemDesctiption = "Item 2";
Hopefully that makes sense..
Thanks.
you can do like this in LINQ, if you write it this way:
string SelectedItemDescription = (from list in ItemList
where list.Value = model.ItemCode
select list.Text).FirstOrDefault();
or you can Use Extension Methods, If it will always return a single element then you can use SingleOrDefault():
SelectListItem SelectedItemDescription = ItemList.SingleOrDefault(item=>item.Value == model.ItemCode);
if(SelectedItemDescription !=null)
{
string Key = SelectedItemDescription .Value;
string Text = SelectedItemDescription.Text;
}
If it will return a multiple objects then you can use FirstOrDefault():
SelectListItem SelectedItemDescription = ItemList.FirstOrDefault(item=>item.Value == model.ItemCode);
if(SelectedItemDescription !=null)
{
string Key = SelectedItemDescription .Value;
string Text = SelectedItemDescription.Text;
}
UPDATED:
As #CodeGeek suggested in comments, you can also do like this:
string SelectedItemDescription =ItemList
.SingleOrDefault(item=>item.Value == model.ItemCode)!=null ? ItemList.SingleOrDefault(item=>item.Value == model.ItemCode).Text : String.Empty;
I have a BIT type in a sql database that I want to turn into a DropDownList.
I pass a model to the page which has an active field. This field is either true or false. I'm adding the ability to modify this column in the database.
Is it possible to create a DropDownListFor that will give them the option of "Active" "Disable" and then return the correct value to the database?
#Html.DropDownListFor(m => m.active, ??)
#Model.active
I have never tried this but how about this:
#Html.DropDownListFor(m=> m.active, new List<SelectListItem>() { new SelectListItem { Text = "Active", Value = "true", Selected = Model.active }, new SelectListItem { Text = "Inactive", Value = "false", Selected = Model.active } })
This solution works great. The only thing I had to change for my code is the "Selected" value if the model value is "False"
#Html.DropDownListFor(m=> m.active, new List<SelectListItem>() { new SelectListItem { Text = "Active", Value = "true", Selected = Model.active }, new SelectListItem { Text = "Inactive", Value = "false", Selected = !Model.active } })
It was displaying Active even though it should display Inactive.
I have the following select list
public SelectList StaffList { get; set; }
I am assigning it the following,
inspectorCorrespondences.StaffList = new SelectList(userDetails, "Id", "FullName");
but I want to add in another SelectList item
var systemGeneratedUser = new SelectListItem() { Value = "-1", Text = "System Generated" };
how do I do that?
This isn't necessarily the most elegant solution, but you'll need to cast the SelectList's items to a list of SelectListItems if you want to add another SelectListItem.
var myItems = new List<SelectListItem>();
myItems.Add(new SelectListItem() { Text = "AA", Value = "11" } );
var mySelectList = new SelectList(myItems);
((List<SelectListItem>)mySelectList.Items).Add(new SelectListItem() { Text = "BB", Value = "22" });
According to the documentation, it looks like you need to initialize the SelectList with the values instead of adding them. Passing an enumerable of SelectListItem to the constructor appears to be the expected way.
It looks like you're already doing that in the sense that userDetails is already an enumeration? If so, what type of enumeration is it? You may need to add a line or two of code to transform it to an enumeration of SelectListItem objects, add yours to that, and then use that to initialize the SelectList. Maybe something like this:
var listItems = userDetails.Select(u => new SelectListItem { Value = u.Id, Text = u.FullName }).ToList();
listItems.Add(new SelectListItem() { Value = "-1", Text = "System Generated" });
// or perhaps .Insert() at position 0 instead?
inspectorCorrespondences.StaffList = new SelectList(listItems, "Value", "Text");