I'm still pretty new to .NET, but I think I've read everything there is to read on this subject (including similar questions on SO, which is where I got some of the things I've tried). I feel like I've tried everything possible and I still can't get it to work.
I have a Note class and a Category class. Pretty straightforward, each note has a Category property, so I want to have a dropdown list in my Create view that displays categories. I can get a list to display the category names correctly, but that's it. It keeps telling me there's no IEnumerable in my ViewData called "Categories" when there definitely, 1000% for sure is...
The Create action in my NoteController looks like this:
// GET: Create
public ActionResult Create()
{
SelectList items = (new CategoryService()).GetCategories().Select(c => new SelectListItem
{
Value = c.CategoryId.ToString(),
Text = c.Name
}) as SelectList;
ViewData["Categories"] = items;
return View();
}
And I've tried a few variations in the view:
#Html.DropDownListFor(e=>e.CategoryId , (IEnumerable<SelectListItem>) ViewData["Categories"])
#Html.DropDownList("Categories", "Select a Category")
My Create view uses a NoteCreate model, which has this:
public class NoteCreate {
...
[Display(Name = "Category")]
[Required]
public string CategoryId { get; set; }
And my NoteService has a CreateNote method like so:
public bool CreateNote(NoteCreate model)
{
using (var ctx = new ApplicationDbContext())
{
bool isValid = int.TryParse(model.CategoryId, out int id);
if (!isValid)
{
id = 0;
}
var entity =
new Note()
{
OwnerId = _userId,
Title = model.Title,
Content = model.Content,
CreatedUtc = DateTimeOffset.Now,
Status = model.Status,
CategoryId = id
};
ctx.Notes.Add(entity);
return ctx.SaveChanges() == 1;
}
}
I figured I have to turn the ID into a string for the sake of the dropdown list (because SelectListItem's Value and Text are strings), which is why I parse it back into an int here
I tried attaching the list to the ViewBag instead, and I've tried variations of both DropDownListFor and DropDownList
One of those combinations resulted in a dropdown list actually showing, and I don't remember what it was, but selecting an item resulted in a null being passed to the NoteCreate method (model.CategoryId)
Can anyone help me, and potentially many others who will struggle with this in the future because the documentation is so terrible?
UPDATE:
My controller has been refactored to:
// GET: Create
public ActionResult Create()
{
List<SelectListItem> li = new List<SelectListItem>();
List<Category> Categories = (new CategoryService()).GetCategories().ToList();
var query = from c in Categories
select new SelectListItem()
{
Value = c.CategoryId.ToString(),
Text = c.Name
};
li = query.ToList();
ViewBag.Categories = li;
return View();
}
and my view has been refactored to:
#Html.DropDownList("Categories", ViewBag.Categories as SelectList, new { #class = "form-control" })
This is closer, as I can now load the view and see the Category names in the dropdown. However, when I save, model.CategoryId in my CreateNote method is null, so the CategoryId value isn't actually being passed from the dropdown into the model.
If ViewModel is used in the view then its better to paa the data through model properties to the view. No need to put the collection for Dropdownlist in ViewData or ViewBag.
For the detail way of using Dropdownlist through SelectList and pass to the view through, I would refer an answer I had posted:
MVC C# Dropdown list Showing System.Web.SelectListItem on the model and can not blind to controller
The model passed to your view needs a property for CategoryId.
Your Html Helper is looking for CategoryId here:
#Html.DropDownListFor(e=>e.CategoryId
Ok... I figured it out.
It's so stupid.
The key you use to store the SelectList in your ViewData HAS to be the same as the name of the property on the model, even though you can explicitly tell it to use the list using a different key....
So even if you wanted to use the same SelectList for a few different properties (but process them differently in your service, say), you'd have to pass it to the ViewData redundantly for each property
So instead of passing my SelectList through as ViewBag.Categories, I passed it in as ViewBag.CategoryId, and that worked.
I'm going to go drink a lot of alcohol now.
In Controller
List<SelectListItem> li = new List<SelectListItem>();
var query = from of in your_context.Categories
select new SelectListItem()
{
Value = of.CategoryId.ToString(),
Text = of.Name
};
li = query.ToList();
ViewBag.Category_ = li;
View
<div class="col-md-10">
#Html.DropDownList("Categories", ViewBag.Category_ as List<SelectListItem>, new { #class = "form-control" })
</div>
Related
I want to categorize binding data from database inside of a dropdownlist. My database table has 4 colums: DepartmentId, Name, ParentID and Level. How can I group them according to level?
This is my code:
public ActionResult Departments()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["FormDatabaseEntities"].ToString()))
{
string sql = #"select * from tblDepartment ORDER BY COALESCE(ParentID, DepartmentID)";
var departments = connection.Query<tblDepartment>(sql).ToList();
SelectList list = new SelectList(departments, "DepartmentID", "DepartmentName");
ViewBag.departments = list;
}
return View();
}
.cshtml side:
#Html.DropDownListFor(i => i.ParentID, ViewBag.departments as SelectList, "Seçiniz...", new { #class = "form-control" })
EDIT
I saw in your code snippet that you're using the SelectList class. It has a property DataGroupField. So all you have to do is
SelectList list = new SelectList(departments, "DepartmentID", "DepartmentName");
list.DataGroupField = "Level";
ViewBag.departments = list;
That should do the trick. The SelectList has also some overloaded constructors that let you specify the group field.
original post...
If you're using items of type SelectListItem then you can utilize the Group property to assign a group to each item. This will automatically handle the grouping in your dropdown if you're using the DropDownListFor helper to generate the select list.
Check the documentation of the SelectListItem for more information.
I have this in my controller:
[Route("Checkout")]
public ActionResult Checkout()
{
var sb = new ShoppingBagViewModel();
sb.DestinationCountry = "Netherlands"; // I hoped that this was sufficient
sb.CountriesSl.Single(c => c.Text.Equals("Netherlands", StringComparison.InvariantCultureIgnoreCase)).Selected = true;
return View(sb);
}
Quick watch in Visual Studio confirmed that the CountriesSl (which is of type List<SelectListItem>) has a selected value. (netherlands)
This is my razor view:
#Html.DropDownListFor(m => m.DestinationCountry, Model.CountriesSl)
DestinationCountry is also a string prop in my viewmodel.
I know that there are a lot of similar questions, but I have looked at a lot of them and I just do not see it.
I also tried:
#Html.DropDownListFor(m => Model.DestinationCountry, Model.CountriesSl)
Please do not just give me the answer, but also explain what my mistake is.
edit to make clear what the problem is: I get a nice option list in my razor view, but there is no item "selected" just the first one. When I look at the generated html, there is no selected item too.
edit for Ric
public List<SelectListItem> CountriesSl
{
get
{
List<SelectListItem> _countries = HttpContext.Current.Cache["slCountries"] as List<SelectListItem>;
if (_countries == null)
{
_countries = new List<SelectListItem>();
foreach (string s in System.IO.File.ReadLines(System.Web.Hosting.HostingEnvironment.MapPath("~/tmpwrite/countries.csv")))
{
var tmp = s.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
_countries.Add(new SelectListItem() { Text = tmp[1], Value = tmp[0], Selected = false });
}
HttpContext.Current.Cache.Add("slCountries", _countries.OrderBy(c => c.Text).ToList(), null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(24, 0, 0), System.Web.Caching.CacheItemPriority.Normal, null);
}
return _countries;
}
}
DropDownListFor tries to automatically match the value of the receiving property in the provided enumeration. I also had problems with this in the beginning.
Instead provide any list of entities in the viewmodel. I usually use something like IDictionary<int, string>. Then create the dropdown like this:
Html.DropDownListFor(m=>m.DestinationCountry, new SelectList(Model.Countries, "Key", "Value"))
Then the SelectList will automagically set the correct option where its Key matches DestinationCoutry.
I have created a DropDownList which is initialized from code behind like that :
Code Behind :
List<SelectListItem> ddlIsActivated = new List<SelectListItem>
{
new SelectListItem
{
Text = "Activated",
Value = "0"
},
new SelectListItem
{
Text = "Not activated",
Value = "1"
}
};
ViewBag.ddlIsActivated = ddlIsActivated;
View :
<div class="row">
<div class="form-group col-xs-3">
#Html.DropDownList("IsActivated", ViewBag.ddlIsActivated as List<SelectListItem>, "Default")
</div>
</div>
When I click directly on the search button after the load, the DropDownList has "Default" as the first item and my URL looks like that :
http://localhost:51817/Log?SearchString=&IsActivated=
Is it possible to specify that all parameters with an empty value might not be passed on the URL ?
In case of the DropDownList, is it possible to avoid the param "IsActivated" when the "Default" is Selected ?
Sure one way is when the submit button is clicked or activated, you set the fields that are the default value to disabled. then they don't go in the post.
Example of using jQuery onsubmit:
How to use jQuery to onsubmit and check if the value is 2 - 5 characters?
You can try having a separate viewmodel, with properties for the list of options and the selected option. In the GET request action method, you can populate this list and render the view, and in the POST action method, you can exclude this property during model binding, so that only the selected item property will get bound. Thus, you don't need ViewBag or routeValues or querystring parameters, and your URL will always look clean.
EDIT:
This is how you might do it.
public class ViewModel
{
public int SelectedId { get; set; }
public Dictionary<int,string> OptionList { get; set; }
}
And then in your view,
#Html.DropDownListFor(model => model.SelectedId, new SelectList(Model.OptionList, "Key", "Value"), null, null)
In your GET action method,
Dictionary<int,string> OptionList = GetOptionList(); // Populate from DB
return View(new ViewModel { OptionList = OptionList });
Also, remember to [Bind(Exclude="OptionList")] in your POST action method.
I am working with a list of string items in mvc that needs to be selected from a drop down list. The drop down list is binding fine, and value's are setting fine, but even though the current item being iterated matches an item in the drop down list it isn't being pre-selected as it's value, can anyone point me in the right direction?
#for (var i = 0; i < Model.StringList.Count; i++)
{
if (BL.Helpers.StringHelpers.Validate(Model.DisplayStringSegments[i]))
{
<div id="editor-field">
#Html.DropDownListFor(m => m.StringList[i], Model.PosterOptions, String.Empty, new { })
</div>
}
else
{
<div id="editor-label">#Model.StringList[i]</div>
#Html.HiddenFor(model => model.StringList[i])
}
}
So for this case, the Options is a list of strings holding only one value, "Test" -> set both as Text and Value;
PosterOptions.Add(new SelectListItem() { Text = "Test", Value = "Test" });
Can anyone tell me why the current StringList[i] isn't being pre selected, even though it has the value of "Test" ?
For anyone that comes across this;
I had to "Hack" a solution, I did this by:
Changing my ViewModel's (Model.Options)
List<SelectListItem> to a List<string>
Changing my drop down list selection to the following, forcing the selected value;
<div id="editor-field">
#{
string currentString = Model.StringList.ElementAt(i).ToString();
}
#Html.DropDownListFor(m => m.StringList[i], new SelectList(Model.Options, currentString), String.Empty, new {})
</div>
Perhaps there is a better way, but this works!
Another way could be setting the current selected item during the list creation, like this:
PosterOptions.Add(new SelectListItem() { Text = "Test", Value = "Test", Selected = true });
I had the same issue and your response helped me. I don't think that's a "hack" though. Because in your question you were using the same SelectList for all the dropdownlists so even though you mention you didn't want to create multiple lists for the drop downs I can't see another way when you have multiple drop downs as you need to specify different selected values.
As a small refactoring you can get rid of the temp variable and access the selected value directly like this:
#Html.DropDownListFor(m => m.StringList[i], new SelectList(Model.Options, Model.StringList[i]), String.Empty, new {})
In your example you don't need to distinguish between text and value but in my case it was required. When that's necessary it can be accomplished by providing the value and text field names for the SelectList. For example, say you need multiple dropdowns with Country values such as:
Country class:
public class Country
{
public string Code { get; set; }
public string Name { get; set; }
}
Model:
public List<string> CustomerCountryList { get; set; }
public IEnumerable<Country> CountryList { get; set; }
and the View:
#Html.DropDownListFor(m => m.CustomerCountryList[i], new SelectList(Model.CountryList, "Code", "Name", Model.CustomerCountryList[i]))
I'm creating a question/answer page that contains multiple object types (radio button, dropdown, check boxes).
Id QuestionText AnswerId AnswerText ObjectType
1 text one 1 Personal DropDown
1 text two 2 Business DropDown
2 Text three 3 Direct Deposit CheckBox
2 text four 4 Some Answer CheckBox
I have a model that contains a list of all questions, answers, and object types.
How can I populate (as an example) the dropdownlistfor with only two items out of the list, then populate a group of related checkboxes, then populate a group of related radio buttons?
The dropdownlistfor looks to enumerate on a model.
My code which doesn't work:
#if (Model != null)
{
for (int i = 0; i < Model.Count; i++)
{
if (Model[i].AdditionalQuestionTypeId == 1)
{
#Html.DropDownListFor(model => model[i].AdditionalQuestionId, ((IEnumerable<Curo.Web.InterAcct.Models.AdditionalQuestionAnswerModel>)Model[i].AnswerText)
.Select(option => new SelectListItem
{
Text = (option == null ? "None" : option.Description),
Value = option.Id.ToString(),
Selected = (Model != null) && (option.Id == Model[i].AdditionalQuestionId)
}), "Choose...")
It's not the best idea to clog up your view with a bunch of logic. That is actually not the desired approach of MVC.
Use this logic in your controller and not the View.
Create you options in the controller that you want for the desired scenario and set the options to the property on the Model. Then your view will simply bind to that property.
public class MyModel {
public string MyValue { get; set;}
public List<SelectListItem> Options { get; set; }
}
public ActionResult MyAction(){
MyModel model = new MyModel();
// populate options here
model.Options = new List<SelectListItem>();
return View(model);
}
Then your view:
#Html.DropDownListFor(m => m.MyValue, m => m.Options)
It's hard to tell from your code, but I think you are almost there. You need to do the dropdownlistfor statement with an IEnumerable, but your syntax appears to be off:
#Html.DropDownListFor(model => model[i].AdditionalQuestionId, new SelectList(Model.Answers.Where(f=>**Some code to select what values you want**), "Id", "Description", Model[i].AdditionalQuestionId))
It would help to see an error message, but I think it is just syntax at this point.