I have this MultipleSelectList in my view:
#Html.ListBoxFor(s => s.Id,
new MultiSelectList((IEnumerable<SelectListItem>)ViewData["ddlList"], "Value", "Text", Model.Id),
new { #style = "margin-top:250px", multiple = "multiple" })
This list is populated here
#{
using (var b = new Entity())
{
ViewData["ddlList"] = b.Table.Select(e => new SelectListItem()
{
Value = e.Id.ToString(),
Text = e.Name
}).ToList();
}
}
This is my model
public string Name { get; set; }
public int[] Id { get; set; }
The problem is that when I select multiple options, only the first one gets to my controller like this:
int[] value = modelObj.Id;
modelObj.Id;--stores my selected values
Someone knows how can I solve this?
Selected values will be array of string not array of int. So, change the type of Id property to type of string[]:
public string[] Id { get; set; }
Related
I have the below code which pass 2 values (ProductId and ItemName) to SelectList (Dropdownlist) located on the View.
I need a way to pass 3 values (ProductId, Price, and ItemName).
public class Product
{
public int ProductId { get; set; }
public int Price { get; set; }
public string ItemName { get; set; }
public IList<ProductInvoice> ProductInvoices { get; set; }
}
public IActionResult Create()
{
IEnumerable<SelectListItem> items = _context.Products.Select(c => new SelectListItem
{
Value = c.ProductId.ToString() + "-" + c.Price,
Text = c.ItemName
});
ViewData["Products"] = items;
return View();
}
Code to generate drop down list:
#Html.DropDownList("SelectedProducts",new SelectList((System.Collections.IEnumerable)ViewData["Products"], "Value", "Text", new { #class = "form-control dropdown-list" }))
You can join every property you need by a delimiter and pass it to Value. Like this:
IEnumerable<SelectListItem> items = _context.Products.Select(c => new SelectListItem
{
Value = String.Join("$", new string[] { c.ProductId.ToString(), c.Price }),
Text = c.ItemName
});
and when fetching back, you can perform a split with the delimiter. Like this:
var myVal = value.Split("$");
string productId = myVal[0];
string price = myVal[1];
You made almost right thing, only if you want to see the price you have to concat it with Name, not with Id
public IActionResult Create()
{
var selectListItems= _context.Products.Select(c => new SelectListItem
{
Value = c.ProductId.ToString(),
Text = c.ItemName+ " - " + c.Price.ToString()
});
var items= new SelectList( selectListItems,"Value", "Text");
ViewData["Products"] = items;
var model=new Product();
return View( model);
}
and fix the view
#Html.DropDownListFor(m=> m.ProductId, (SelectList) ViewData["Products"], new { #class = "form-control dropdown-list" }))
I have this model:
public class CampoTipoDocumentoViewModel
{
public int TipoDocumentoId { get; set; }
public string[] CamposId { get; set; }
private List<MiddleTier.Models.ICampo> _todosCampos;
public IEnumerable<SelectListItem> TodosCampos
{
get
{
foreach (var campo in _todosCampos)
yield return new SelectListItem { Text = campo.Nombre, Value = campo.Id.ToString() };
}
}
public void SetFields(List<MiddleTier.Models.ICampo> campos)
{
_todosCampos = campos;
}
}
In controller, CamposId property is assigned with elements which has to be selected in the view.
Controller also calls SetFields method populating _todosCampos to the whole list of records in the system.
The idea is to create a View with a SELECT that has initially some records selected.
This is my view:
#Html.DropDownListFor(m => m.CamposId, Model.TodosCampos, new { #class = "form-control", multiple = "multiple", width = "100%" })
The fact is that the HTML SELECT element is created with the list, but no option is selected.
For example, if _todosCampos contains:
Text = "One", Value = "1"
Text = "Two", Value = "2"
Text = "Three", Value = "3"
Text = "Four", Value = "4"
and CamposId contains:
Array of "2", "4"
I need the view to create a SELECT with those 4 options, and option 2 and 4 to be initially selected.
How can I achieve this?
Thanks
Jaime
In order to use <select> element with multiple="multiple" attribute, you need to declare List<string> property:
public List<string> CamposId { get; set; }
And then use ListBoxFor helper instead of DropDownListFor:
#Html.ListBoxFor(m => m.CamposId, Model.TodosCampos, new { #class = "form-control", multiple = "multiple", width = "100%" })
If you want to set some option values are selected by default, then set Selected property into SelectListItem:
public IEnumerable<SelectListItem> TodosCampos
{
get
{
foreach (var campo in _todosCampos)
{
// assumed you have 'campo.IsDefault' which is boolean property
yield return new SelectListItem
{
Text = campo.Nombre,
Value = campo.Id.ToString(),
Selected = campo.IsDefault // set default selected values
};
}
}
}
Note: Usually ID property contain integer values, you can try for List<int> CamposId depending on actual ID data type in database.
Here is my Model
public class NewsViewModel
{
public NewsViewModel()
{
this.Categories = new List<Category>();
}
public int NewsId { get; set; }
[Required(ErrorMessage = "Please enter News Title")]
public string NewsTitle { get; set; }
public IEnumerable<Category> Categories { get; set; }
}
I need to set Selected to be True if id exists in NewsViewModel.Categories collection
private IEnumerable<SelectListItem> GetCategories()
{
return db.Categories .Select(s=>new SelectListItem {
Value=s.Id.ToString(),
Text=s.Name,
Selected = model.Categories.Select(x => x.CategoryId).Contains(s.CategoryId);
}
And in View:
#Html.ListBoxFor(s => s.SelectedCategoriesIds, #Model.AllCategories, new { id = "DropDownList2", multiple = "multiple", #class = "form-control" })
See this question; you cannot use Contains with non-primitive types (like Category in this case). A different way to write your query that works around this limitation would be:
return db.Categories.Select(s=>new SelectListItem {
Value=s.Id.ToString(),
Text=s.Name,
Selected = model.Categories.Exists(z => z.CategoryId == s.CategoryId);
}
Instead of selecting the CategoryIds from model.Categories and then checking if s.CategoryId is in that list, we can check to see if there Exists() an item in model.Categories for which the CategoryId is the same as s.CategoryId.
Add the condition on the "selected" as a question mark condition.
var listSiteId = (from site in db.GetSiteId().ToList()
select new SelectListItem
{
Value = site.SITEID,
Text = site.NAME,
Selected = (dimension.DISPLAYVALUE == site.SITEID) ? true : false,
}).ToList();
ViewBag.SiteId = listSiteId;
I'm trying to do this:
This is my ViewModel and Model:
public class OpeningYearViewModel
{
public int OpeningYearId { get; set; }
public string Description { get; set; }
public List<Grade> GradesList { get; set; }
}
public class Grade
{
public int GradeId { get; set; }
public string Name { get; set; }
public int CurrencyId { get; set; }
public int Cost { get; set; }
}
This is my Controller. I build a SelecList here and pass it to the view through the ViewBag
OpeningYearViewModel viewmodel = new OpeningYearViewModel {
OpeningYearId = 1,
Description = "2015 - II",
GradesList = new List<Grade>
{
new Grade { GradeId = 1, Name = "Grade 1", CurrencyId = 1, Cost = 100 },
new Grade { GradeId = 2, Name = "Grade 2", CurrencyId = 2, Cost = 200 },
new Grade { GradeId = 3, Name = "Grade 3", CurrencyId = 2, Cost = 150 }
}
};
SelectList list = new SelectList(
new List<SelectListItem>
{
new SelectListItem { Text = "S/.", Value = "1"},
new SelectListItem { Text = "$", Value = "2"},
}, "Value" , "Text");
ViewBag.currencyList = list;
return View(viewmodel);
And in my View I need a DropDownListFor for every item on GradesList so I do this:
#model Test.Models.OpeningYearViewModel
#for(int i = 0; i < Model.GradesList.Count; i++)
{
#Html.DropDownListFor(x => x.GradesList[i].CurrencyId, new SelectList(ViewBag.currencyList, "Value", "Text"))
#Model.GradesList[i].CurrencyId //This is just to know the CurrencyId on every item.
}
I'm getting every select correctly rendered, but I can't get the correct option selected on the page load:
render of view
It is possible to do what I'm trying to do and I'm doing something wrong, or DropDownListFor works in a different way?
Thanks!
I can't understand why this is happening but you can workaround it by setting explicitly the selected value. This can be done by passing Model.GradesList[i].CurrencyId as fourth parameter to the SelectList's constructor:
#for(int i = 0; i < Model.GradesList.Count; i++)
{
#Html.DropDownListFor(x => x.GradesList[i].CurrencyId,
new SelectList(ViewBag.currencyList, "Value", "Text", Model.GradesList[i].CurrencyId))
}
Since, the DropDownListFor is used in loop to generate indexed inputs; so generate input controls will be generated with name as "GradeList[0].CurrencyId", "GradeList[1].CurrencyId"......Due to this framework will not bind the selected value in Select List as it is unable to get the value in reflection for selection. That's why you have to use the following SelectList constructor to set the selected value.
public SelectList(
IEnumerable items,
string dataValueField,
string dataTextField,
object selectedValue
)
However, if your DropDownListFor is bind to a simple expression like
#Html.DropDownListFor(model => model.CurrencyId, new SelectList(ViewBag.Items, "Value", "Text"))
then selection will work automatically.
I am trying to create a drop down list but it gives me an error saying 'Cannot implicitly convert type 'string' to 'System.Web.Mvc.SelectList'. My code is below:
Application Database Model:
public string dropdown{ get; set; }
Application View Model:
public SelectList dropdown{ get; set; }
ApplicationService.cs:
public static SelectList GetDropdownList(string currSelection)
{
List<SelectListItem> list = new List<SelectListItem>();
list.Add(new SelectListItem { Value = "1", Text = "firstvalue" });
list.Add(new SelectListItem { Value = "2", Text = "secondvalure" });
list.Add(new SelectListItem { Value = "3", Text = "All of the Above" });
return new SelectList(list, "Value", "Text", currSelection);
}
in my controller i am calling:
applicationviewmodel.dropdown= ApplicationService.GetDropdownList(null);
and then trying to save it in database as:
ApplicationDatabaseModel.dropdown= applicationviewmodel.dropdown;
This is where i get this error.
In my view i have:
#Html.DropDownListFor(x => x.dropdown, applicationviewmodel.dropdown)
I am not sure how to make this work.
I find it's easier to just have a List as part of your model and use a simple linq statement. Simple example below for a countries drop down:
assuming you have a model like
public class MyModel()
{
public int CountryId { get; set; }
public List<Country> Countries { get; set; }
}
and a Country class of
public class Country()
{
public int Id { get; set; }
public string Name { get; set; }
}
in your view you can then do the following:
#Html.DropDownListFor(m => m.CountryId,
Model.Countries.Select(x =>
new SelectListItem { Text = x.Name, Value = x.Id.ToString(), Selected = Model.CountryId == x.Id }, "Please Select...", null)
This:
#Html.DropDownListFor(x => x.dropdown, applicationviewmodel.dropdown)
..is incorrect. It is trying to store the selected item into the SelectList instance.
What you want is a string variable on the view model that this value is selected into:
public class ApplicationViewModel {
public SelectList DropDown { get; set; }
public string SelectedDropDownValue { get; set; }
// .. the rest of the properties here
}
Then your view becomes this:
#Html.DropDownListFor(x => x.SelectedDropDownValue, Model.DropDown)
This says "store the selected value into SelectedDropDownValue".
Then, you need to change how you build your SelectList. Value is what gets posted to your property.. Text is what is displayed in the browser.
So this:
list.Add(new SelectListItem { Value = "1", Text = "firstvalue" });
list.Add(new SelectListItem { Value = "2", Text = "secondvalure" });
list.Add(new SelectListItem { Value = "3", Text = "All of the Above" });
..has to be this:
list.Add(new SelectListItem { Value = "firstvalue", Text = "firstvalue" });
list.Add(new SelectListItem { Value = "secondvalue", Text = "secondvalure" });
list.Add(new SelectListItem { Value = "all of the above", Text = "All of the Above" });
..because they are strings (unless of course, you want the numbers to be posted back).
Then, finally, your controller code becomes this:
// assign the string value to the string property
ApplicationDatabaseModel.dropdown = applicationviewmodel.SelectedDropDownValue;