how to add DropDownList to ASP.NET Identity? - asp.net-mvc

I started to use ASP.NET Identity, and my question is how can I add a dropdownlist for gender (Male and Female) to my database?
I added a string value called "Gender" to my "AspNetUsers" table(with migrations in the cmd)
and I want to save there the values from the dropdownlist..
I'm using MVC but I can use Web Forms as well.
I saw this link: MVC4 Dropdown menu for Gender and I didn't understand how does it connect to the databse.
Thanks in advance.

In Controller
ViewBag.gender=new List<SelectListItem>(){
new SelectListItem(){Text="Male", Value="Male"},
new SelectListItem(){Text="Female", Value="Female"}
} ;
In view
#Html.DropDownListFor(
x => x.GenderId,
new SelectList(ViewBag.gender as List<SelectListItem>, "Value", "Text"))

Related

Asp.net MVC DropDownList Add and Update

I am quite new to this as my background is ASP.net Web Form. I am faces with 3 issues as followed:
Dropdownlist postback and other dropdownlist is showen once the user selected the 1st dropdownlist.
2nd, I am trying to pass the dropdownlist selected value in adding data
#Html.DropDownList("CategoryID", ViewData["Category"] as List)
I am trying to update the data based on the selectedvalue
#Html.DropDownList("CategoryID", ViewData["Category"] as List)
I am able to populate it from the database.
Any advice would be helpful. Many thanks
The answer to this question is
#Html.DropDownListFor(model=> model.CategoryID, ViewData["Category"] as List<SelectListItem>)
and you use FormMethod.Post

MVC 5 Entity Framework 6 project with Database first, ViewBag, dropdown for foreign key and databinding

I am experimenting with the Entity Framework in a new mvc project, so I created a database, and started out with a database first approach. One of the tables I created had a foreign key to another table, and when the model got created, a virtual property was created to address the key value.
Then I had Visual studio create the controller / views with all the crud. Everything is working fine, but I want to change the dropdowns to Kendo.
The Controller is using ViewBag properties to send the foreign key data back to the view like so:
ViewBag.CourtId = new SelectList(db.Courts, "Id", "Name", tournament.CourtId);
the dropdown looks like this:
#Html.DropDownList("ProviderId", null, new {#class = "form-control"})<br />
I can't figure out how the viewBag data is being bound to the dropdown, nor have I been able to figure out how to substitiute a kendo dropdownlist?
How is this ViewBag data being bound to the dropdown?
so, I found the answer here: Click this Link
Just sub out:
#Html.DropDownList("ProviderId", "Select a Value")
with
#(Html.Kendo().DropDownListFor(model => model.ProviderId)
.OptionLabel("Select a Value")
.BindTo(ViewData["ProviderId"] as SelectList))

Cascading dropdown lists in ASP.NET MVC 5

I am wondering if there's some new helper or method introduced in ASP.NET MVC 5 to implement cascading dropdown lists. I know a way to implement cascading dropdownlist behavior in MVC 3 and MVC 4 that is by using a JSON call
http://www.dotnet-tricks.com/Tutorial/mvc/HL53191212-Custom-Validation-for-Cascading-Dropdownlist-in-MVC-Razor.html
So anyone knows a better way to implement cascading dropdownlists in MVC 5?
I know that this is an old question but somebody still may find it useful
I was searching for the same thing but din't find anything stable and useful so I ended up implementing it by myself:
Please take a look at Mvc.CascadeDropDown helper that I created.
It works with all MVC versions starting from MVC3 and doesn't require any client side libraries(It uses plain vanilla JavaScript).
The usage is very simple:
#using Mvc.CascadeDropDown
//First simple dropdown
#Html.DropDownListFor(m=>m.SelectedCountry, Model.Countries,
"Please select a Country", new {#class="form-control"})
//Dropdown list for SelectedCity property that depends on selection of SelectedCountry property
#Html.CascadingDropDownListFor(
expression: m => m.SelectedCity,
triggeredByProperty: m => m.SelectedCountry, //Parent property that trigers dropdown data loading
url: Url.Action("GetCities", "Home"), //Url of action that returns dropdown data
actionParam: "country", //Parameter name for the selected parent value that url action receives
optionLabel: "Please select a City", // Option label
disabledWhenParrentNotSelected: true, //If true, disables dropdown until parrent dropdown selected
htmlAttributes: new { #class = "form-control" }) //Html attributes
Hopefully it will be helpful for some of you
No, there are no new helpers or methods in MVC 5 to help.
The Ajax HTML helpers have been largely ignored in the update. There are some things that may help with stuff related to this:
There is a new #Html.EnumDropDownListFor() HTML helper to populate a dropdown from an Enum.
The Attribute routing functionality of the has been improved and now works with the Web API so it is much easier to map URLs to API calls.
You can now pass in html attibutes in the EditorFor Html helper #Html.EditorFor(m => m.FieldName, new { htmlAttributes = new { #class = "form-control" } })
I implemented cascading dropdowns last week and used the tried and true JSON call you mentioned. I like to use this jQuery plugin in conjunction with the Web API v2 with the new attribute routing.

Dropdown in ASP.NET MVC 3

I am new to MVC so probably confused . can somebody please explain me the dropdown in razor.my questions are-
What is the difference in dropdownlist and dropdownlistfor
how do i Pass ID column of my database table as value and NAME column as text.
How do i add "other" to the dropdownlist.
how do i access the selectedlistitem in code behind.
if possible please explain with an example.
DropDownList is generated by code like this:
#Html.DropDownList("PersonId", new SelectList(Model.People, "Id", "Text");
On the other hand, DropDownListFor is generated like this:
#Html.DropDownListFor(m => m.PersonId, new SelectList(Model.People, "Id", "Text")
Problem with DropDownList is that it has a magic string and if you decide to refactor the model later on, there's a high change you'll forget to change the magic string too.
You could do a LINQ query like this:
var datalist = New SelectList(from x in _peopleService
select new SelectListItem { Text = x.Name, Value = x.Id});
If you don't have a service or an ORM between it you need to apply it to your situation, but you can generate a list like that.
After nr 2, you can
datalist.Add(new SelectListItem() { Text = "Other", Value = "-1"});
Also you have to put that datalist in your viewmodel/model that is passed to the View, so you can generate a selectlist item with that. In this case you could just do:
#Html.DropDownListFor(x => x.PersonId, Model.PersonList);
if you stored the list as PersonList in Model.
In your Viewmodel (Well, model in mvc) you have a property where the selected item will be stored, look at the 1st question - In this instance the selected item's id will be stored in the PersonId property.

ASP.Net Html.DropDownList Selected Element not Selected

I have a site that was using ASP.Net MVC Beta 5, and I have just upgraded it to ASP.Net MVC 1.0. I am having trouble with the selected item in a drop down list.
The follow person has a similar question (Html.DropDownList in ASP.NET MVC RC (refresh) not pre-selecting item) but I there is no answer (other than it might be a bug)
My Controller method looks as follows:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult View(Guid id)
{
IntegrationLogic logic = new IntegrationLogic(new IntegrationLinq());
CompanyLogic companyLogic = new CompanyLogic(new CompanyLinq());
IntegrationContainer container = new IntegrationContainer();
container.Sources = logic.GetImportSource(id);
container.Companies = companyLogic.GetCompanies(); // Returns a IList<company>
container.SourceActions = logic.GetAllSourceActions(); // Returns an IList<SourceAction>
container.SinkActions = logic.GetAllSinkActions();
container.SuccessActions = logic.GetAllSuccessActions();
container.FailureActions = logic.GetAllFailureActions();
container.Actions = logic.GetAllActions();
container.Watchers = logic.GetAllWatcherActions();
container.ChainActions = logic.GetAllChainActions();
return View("View", container);
}
The view is a strongly typed against the Model as follows
public partial class View : ViewPage<IntegrationContainer> {}
The problem area in the view template is :
<label for="Companies">Company: </label><%=Html.DropDownList("Companies",
new SelectList(ViewData.Model.Companies, "id", "name", item.CompanyID))%>
I am creating a Dropdown List, the selected item never actually gets selected - and that is the problem. "item.CompanyID" is a Guid, "id" is a Guid and "name" is a string on the company object supplied in the IList that is held in the ViewData.Model.Companies instance.
Is this actually a bug ?- I find it hard to understand why this is still present in ASP.Net MVC... I would be totally happy if it is something I have done.
Regardless, what would be the suggested work around?
Thanks
It turns out that if the name of your control via Html.DropDownList is the same name as the collection object it causes an issue with ASP.Net MVC.
So if I change the following code:
<label for="Companies">Company: </label><%=Html.DropDownList("Companies",
new SelectList(ViewData.Model.Companies, "id", "name", item.CompanyID))%>
to:
<label for="Companies">Company: </label><%=Html.DropDownList("company",
new SelectList(ViewData.Model.Companies, "id", "name", item.CompanyID))%>
all now works. This is because the name of collection on the model was Model.Companies.... bonkers... also note, that changing the case of the name of the control from "Companies" to "companies" does not work either (which makes sense I suppose).
I could change the Model, but as most of it is built using Linq-to-SQL I think it is easier to change the names of the Html elements.

Resources