Select an Item in Html.DropDownListFor() with MVC - asp.net-mvc

I have a table(Collection) of Kinds And in Edit View (in MVC) Need this Kinds in DropDown, Also I Need The DropDown Select Old Kind as Default, But there is some Problems.
This is my Select List:
var Kinds = db.Kinds
.Where(n => n.Member.Id == owner.Id)
.Select(k =>k.Title);
var selectList = new SelectList(Kinds);
Also The Default Kind(Value) is :
var Selection= db.Kinds
.Single(k => k.Numbers
.Any(no => no.Id == Model.Id)).Title;
And this is My DropDown:
#Html.DropDownListFor(model => model.NumberKind.Title, selectList)
When I Define DropDown Like following the new member added at the first of dropdown items:
#Html.DropDownListFor(model => model.NumberKind.Title, selectList, Selection)
For ex if the items be [Mobile, Home, Work]
And Selection is [Home]
With above Code the items will be like this: [Home, Mobile, Home, Work]
but I don't need new item, I just want Select Home From The Main Items
How Can I do this?
Thank you

You're currently providing a "default" value for your drop down list. The intended purpose of the dropdownlistfor constructor you are using would be to add an item like "Select Phone" to your drop down list.
Use simply:
#Html.DropDownListFor(model => model.NumberKind.Title, selectList)
Similar question here for reference: DropDownList setting selected item in asp.net MVC

Related

Razor mutliselect dropdown- Set values as selected in the multiselect dropdown in ASP.Net MVC

I need a way to set already selected values in a multi select dropdown in a edit view in a web application (ASP.NET, Mvc)
<div class="form-group">
<label>Choose Categories</label>
#Html.DropDownListFor(model => model.SelectedValues, new MultiSelectList(Models.GetCategoryList().OrderBy(c => c.Value), "Key", "Value"), "Select Category", new { #class = "chzn-select form-control", multiple = "multiple" })
</div>
this is view code, here I passed selected ids in to selected values array.(when I return values from DB , assign those in to a array then assign to here.
I need a edit view like this
please anyone suggest way to do this
Thank you.
To generate this view you can use select2 jquery plugin. Check out the following link.
https://select2.github.io/examples.html#multiple

How to display empty dropdown in view page

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" })

MVC models to populate KendoUI Multiselect - preloading selected items

I currently have the multiselect working the way I want it with one exception. Preloading previously selected items.
My view looks:
<div class="editor-field">
#Html.LabelFor(model => model.UntaggedPersons)
#Html.ListBoxFor(model => model.PeopleToTag,
new SelectList(Model.CommitteeMembers, "PersonId", "FullName"))
</div>
JS:
$("#PeopleToTag").kendoMultiSelect({ "dataTextField": "FullName", "dataValueField": "PersonId", "placeholder": "Tag or Untag Persons to Action Item" });
PeopleToTag is an IEnumerable<int> int that posts to the controller
CommitteeMembers consists of a persons First, Last, FullName, and PersonId
This works perfectly, I can manipulate who is tagged in the post to the controller.
Now I want to add the functionality of preselecting those that are currently tagged.
How do I add a preselected Value? I have a model with:
model.TaggedPersons consists of a persons First, Last, Fullname, PersonId.
How do I wire up the javascript to display the model.TaggedPersons as the already selected value?
I don't mind using the MVC wrapper approach if it is easier.
I've tried something like:
<div class="editor-field">
#Html.LabelFor(model => model.TaggedPersons)
#(Html.Kendo().MultiSelectFor(model => model.PeopleToTag)
.Name("PeopleToTag")
.Placeholder("These people are Tagged")
.DataTextField("FullName")
.DataValueField("PersonId")
.BindTo(Model.CommitteeMembers)
.Value(Model.TaggedPersons)
)
</div>
However this isn't even rendering a kendo multiselect for me at present. That's the general thought of how I want to approach it though.
Any help is much appreciated!
Change:
DataTextField("FullName") to DataTextField("Text")
DataValueField("PersonId") to DataValueField("Value")
SelectList uses "Text" and "Value", respectively. You already filled the fields of the select list with FullName and PersonId when the selectlist was created.

Specify default value for ASP MVC 3 DropDownListFor

I've done this a hundred times but not sure what is going on here. I have a DropDownListFor that I populate in the controller like so
var mktsegments = from p in db.ChannelMarketSegment
where (p.ChannelCode != "0")
select p;
ViewBag.Pendist = new SelectList(mktsegments, "Pendist", "Pendist");
And in the view, I am attempting to set the default value of this drop down list with the Pendist value.
EDIT: Pendist is a field that exists in each item pulled into mktsegments via the Linq query.
<div class="M-editor-label">
#Html.LabelFor(model => model.Pendist)<span class="req">*</span>
</div>
<div class="M-editor-field">
#Html.DropDownListFor(model => model.Pendist,
(SelectList)ViewBag.Pendist, new { onchange = "ChangeChannel()" })
</div>
However, all this does is set the first value in the list as the default value. If I try to add model => model.Pendist or Model.Pendist as the third paramter in the DropDownListFor like this
#Html.DropDownListFor(model => model.Pendist,
(SelectList)ViewBag.Pendist, model => model.Pendist, new { onchange = "ChangeChannel()" })
I either get the following errors
(for model => model.Pendist)
Cannot convert lambda expression to type 'string' because it is not a delegate type
(for Model.Pendist)
'Model' confilcts with the declaration 'System.Web.Mcv.WebViewPage<TModel>.Model'
You are conflicting with the MVC ModelState. When creating Drow down lists, make sure that your property that holds the selected value is not named the same thing as your list of objects. Also, do not use a lambda for the default value, but rather just use the model item directly.
#Html.DropDownListFor(model => model.Pendist, (SelectList)ViewBag.PendistList,
Model.Pendist, new { onchange = "ChangeChannel()" })

How can I show default message if no data and selected value if data available in dropdownlist in MVC4

I am binding the dropdownlist with some data and display the selected data in dropdownlist also. That works fine with the following code.
#Html.DropDownListFor(modelItem => item.UniformCommentSelected,
new SelectList(item.UniformComment),
item.UniformCommentSelected)
Now I want to add default message "Select" for dropdownlist.
I also want to show the selected value in dropdownlist if any and "Select" if not.
If I put "Select" as third parameter in DropDownListFor then it display "Select" for all users. If I put "Select" as 4th parameter in dropdownlist then nothing happens.
Can any one help me?
Try to use the following code.
#Html.DropDownListFor(modelItem => item.UniformCommentSelected, new SelectList(item.UniformComment),item.UniformCommentSelected == null ? "---Select---" : item.UniformCommentSelected)
in my project i fill my dropdown list like this it's below
#Html.DropDownListFor(model => model.CountryId, new SelectList(Model.Countries, "ID", "Name"), "select", new { #ID = "ddlCountry", #class = "text", Style = "width: 150px;", onchange = "javascript:cascadingdropdown();" })
i think this will help you
I usually bind to a collection of SelectListItem's, and manually add the blank of "Please select one..." option to that collection. In the controller (or in the view right before rendering the droplist), I set .Selected = true for the selected option.

Resources