I am new to mvc and i need some help.
I have this model class:
Public Class PostCategories
Public Property ID() As Integer
Public Property Name() As String
Public Property Slug() As String
Public Property ParentID() As Integer
and I have this dropdownlist in my view:
#Html.DropDownList("IDList", New SelectList(Model, "Id", "ID"), "Selected Parent")
Instead of ID, I'd like to put the Name in the dropdownlist and save the ID to the db. How am i supposed to do it? Need help pls.
Assuming your View Model is a list of PostCategories then just use:
#Html.DropDownList("IDList", New SelectList(Model, "ID", "Name"), "Selected Parent")
As Beyers said, it should be sufficient to change your code to
#Html.DropDownList("IDList", New SelectList(Model, "ID", "Name"), "Selected Parent")
where the second parameter of the SelectList constructor is the value of the selected item you want to be passed upon form submit, and must reflect the name of one of your Model properties, therefore must be "ID" and not "Id".
The second parameter is the item's name displayed in the dropdown, in this case you should use "Name" as it's probably the property you want to use.
Optionally you can pass in a fourth parameter that is the selected value at page load time.
Another approach would be to build your SelectList in the controller and pass it using the ViewBag like this:
ViewBag.PostCategoriesId = new SelectList(yourPostCategoriesList, "ID", "Name");
and then in the View your code would be like:
#Html.DropDownList("PostCategoriesId", string.Empty)
Related
I have my get and post methods . I populate my data during post methods based on some value. When i try to run the program it gives me a here is no ViewData item of type 'IEnumerable' that has the key because there is no data in the dropdown. How can i show empty dropdown and bind the same during post method .
The DropDownListFor template must be provided with an IEnumerable to work properly. If you want an empty list, the best way to provide an empty IEnumerable is to use the Enumerable.Empty<> static method.
Your code would then look like this:
#Html.DropDownListFor(model => model.Name, Enumerable.Empty<SelectListItem>(), "-- Select Name --", new { #class = "form-control" })
Trying to create an editor template using a dropdownlist in MVC4. I can get the dropdownlistfor to work directly in the view as such:
#Html.DropDownListFor(model => model.Item.OwnerId, new SelectList(Model.DDLOptions.CustomerOptions, "Value", "DisplayText"))
But then to "generify" it and put it into an editor template, I cannot get it to work.
Here is what I am trying in my EditorTemplate partial:
#Html.DropDownListFor(model => model, new SelectList(Model.DDLOptions.CustomerOptions, "Value", "DisplayText"))
I am getting the error:
Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'int' does not contain a definition for 'DDLOptions'
Model.DDLOptions.CustomerOptions is of type IEnumerable<DDLOptions<int>>:
public class DDLOptions<T>
{
public T Value { get; set; }
public string DisplayText { get; set; }
}
Does this error have something to do with DDLOptions being a generic?
This line is the problem:
#Html.DropDownListFor(model => model, new SelectList(Model.DDLOptions.CustomerOptions, "Value", "DisplayText"))
Your model is simply an int, based on the code above, but then you're calling new SelectList(Model.DDLOptions.CustomerOptions, "Value", "DisplayText") in the partial too, referencing Model.DDLOptions, which does not exist in your model in the editor template. Your model is just an int.
There are several ways to do this, one of which is creating a custom model class for your item owner and have it contain the ownerID and the DDLOptions. Another would be to stick the DDLOptions in the ViewBag, but I usually stay away from that as I prefer using well-written, view-specific view models.
I hope this helps.
I got an exception when user submitted a form without selecting any value in the dropdownlistfor as follows:
<div class="editor-field">
#Html.DropDownListFor(
x => x.TypeID,
new SelectList(ViewBag.EventType, "ID", "Name"),
"-- Select Event Type --"
)
#Html.ValidationMessageFor(model => model.TypeID)
</div>
I got an exception:
Value cannot be null. Parameter name: items
I think I have to check if user selected a value or make an alert, but I'm not sure how to make it.
You should use a nullable type for the TypeID property on your view model if you want to handle the non-selected case in your drop down list ("-- Select Event Type --"):
public int? TypeID { get; set; }
You are getting the exception because if the user doesn't select any option in the dropdown, the default one is used:
<option value="">"-- Select Event Type --"</option>
Notice how the value is empty string. This obviously cannot be bound to a non-nullable integer type on your model.
I have a dropdownlist where a user can select a care provider. In the database the value for this is null however in the application, a value is selected. What do I need to do to have the null value appear as a blank? I thought this was the default behavior. I changed to using strongly typed lists in my view model instead of the viewbag and this may have broken at that time.
Here is the view markup:
<div class="editor-label">
#Html.LabelFor(model => model.PsychologistId, "Psychologist")
</div>
<div class="editor-field">
#Html.DropDownListFor(x => x.PsychologistId, Model.ListPsychologists)
#Html.ValidationMessageFor(model => model.PsychologistId)
</div>
Here is the property from the view model:
[DisplayName("Psychologist")]
public Nullable<int> PsychologistId { get; set; }
Here is the relevant part of my controller:
model.ListPsychologists = new SelectList(XXX, "Id", "DisplayName");
return this.View(model);
where XXX is just the LINQ expression with filtering and sorting criteria. It's been omitted for clarity and space.
The model passed from the controller to the view has PsychologistId being null. And the SelectedValue property on model.ListPsychologists is null.
if PsychologistId is an int, it will assign 0 value to it since int is not a nullable type.
Show your model and controller if my assumption above is not true.
Does your SelectList contain entry with Id==null? I doubt that. And that means that you will have no select list entry matching your value, so browser will select first one available.
Solution: explicitly add entry with Id = null to your SelectList.
I've had these issues before, and what I did to fix it was send the base collection of what I want to fill the dropdown with in the viewmodel, instead of a SelectList. So:
model.ListPsychologists = XXX;
Then in the view:
#Html.DropDownListFor(x => x.PsychologistId, new SelectList(Model.ListPsychologists, "Id", "DisplayName", Model.PsychologistId))
Please verify the SelectList constructor overload I used in MSDN, I'm typing this from memory. Basically you give it the collection of items to use in the SelectList, then the name of the text and value properties (just like you did in your action method code), and the fourth parameter is the value to set the drop down to.
Edit: Also, there is an overload for the DropDownListFor to add a default item in the menu. Like, "Select One".
I'd like to create a dropdownlist from following structure
List<KeyValuePair<long, string>> sponsori =
new List<KeyValuePair<long, string>>();
Now I want the selectlist to have the pair's long as data value, the string as text value and the selected item, if I have only access to the long of the selected item.
THanks in advance.
In your action code
yourViewModel.Sponsori= new SelectList(sponsori, "Key", "Value")
In your view code
<%=Html.DropDownList("yourSelectid", Model.Sponsori) %>
ViewData["selectList"] = new SelectList(sponsori, "Key", "Value");
And then on the page:
<%= Html.DropDownList("selectList") %>
You can also check out Rendering a Form in ASP.NET MVC Using HTML Helpers for a similar example (and more documentation).