Part 35 How to set an item selected when an asp net mvc dropdownlist is loaded

preview_player
Показать описание
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

All ASP .NET MVC Text Articles

All ASP .NET MVC Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

To have the "IT" department selected, when the departments are loaded from tblDepartment table, use the following overloaded constructor of "SelectList" class. Notice that we are passing a value of "1" for "selectedValue" parameter.
ViewBag.Departments = new SelectList(db.Departments, "Id", "Name", "1");

If you run the application at this point, "IT" department will be selected, when the dropdownlist is rendered. The downside of hardcoding the "selectedValue" in code is that, application code needs to be modified, if we want "HR" department to be selected instead of "IT".

Let's now discuss, the steps required to drive the selection of an item in the dropdownlist using a column in tblDepartment table.
Step 1: Add "IsSelected" bit column to tblDepartment table
ALTER TABLE tblDepartment
ADD IsSelected BIT

Step 2: At this point, this column will be null for all the rows in tblDepartment table. If we want IT department to be selected by default when the dropdownlist is loaded, set "IsSelected=1" for the "IT" department row.
Update tblDepartment Set IsSelected = 1 Where Id = 2

Step 3: Refresh ADO.NET Entity Data Model

Step 4: Finally, make the following changes to the "Index()" action method in "HomeController" class.
public ActionResult Index()
{
SampleDBContext db = new SampleDBContext();
List[SelectListItem] selectListItems = new List[SelectListItem]();

foreach (Department department in db.Departments)
{
SelectListItem selectListItem = new SelectListItem
{
Text = department.Name,
Value = department.Id.ToString(),
Selected = department.IsSelected.HasValue ? department.IsSelected.Value : false
};
selectListItems.Add(selectListItem);
}

ViewBag.Departments = selectListItems;
return View();
}

Run the application and notice that, "IT" department is selected, when the dropdownlist is loaded.

If you now want "HR" department to be selected, instead of "IT", set "IsSelected=1" for "HR" department and "IsSelected=0" for "IT" department.
Update tblDepartment Set IsSelected = 1 Where Id = 2
Update tblDepartment Set IsSelected = 0 Where Id = 1
Рекомендации по теме
Комментарии
Автор

How to caapture the selected Id of dropdownlist

thearunnagar
Автор

Dude! I simply have to echo what everybody else has been saying.  This videos are excellent.  Right to the point. Concise.

dbibbyma
Автор

Thank you sir, I am new to MVC and your videos helping me alot to understand the concepts.

rakeshshrivastava
Автор

So for all those who are encountering an error in the View that might have been developed as a part of the scaffolding operation during the View Generation..please go to the department block, in that specific view and check out what is the ViewBag variable that is being fetched. Might be the variable in this case that Venkat has used to store the final result in the controller ie Departments might be something different from what is present in the View ie maybe DepartmentID.

gurunathrao
Автор

Hi venkat... i see your all video..is it possible to do such thing like hide panel etc..using mvc ? each button click is doing get and post thing....nothing like hide .update label..
etc ...and each have its button have new pages.. not like multiple button on same page and you can create way that submit all information on last button..(next which move to next panel but do nt submit any thing in database but at last button of flow submit all information together..i also do not see post back

ankit
Автор

Hi sir,
your tutorial videos and concept is very clear to understand and learn, thank you.

santoshgiri
Автор

How to set an item selected when an asp net mvc dropdownlist is loaded for Item-Model(primary key), ItemDetails-Model(foreign key) in ecommerce product. if its edited Item have many itemdetail of same Product dropdownlist of different product items selected.how can i set ViewBag.ProductId = new SelectList(db.Product, "ProductId ", "ProductName", " ? "); List of Itemdetails can't be apply.

sivabalan
Автор

In case more than one is selected value is true then what is the value selected in the drop down?

gmanickam
Автор

Item not selecting from the database. If I put breakpoint and check, the values of dep.Selected is setting correctly, still not showing selected in the dropdownlist. Please let me know what's the issue. Following is the code.

public ActionResult Index()
{
Models.MVCPracticeEntities1 obj = new
ViewBag.Departments = obj.tblDepartments.ToList();
List<SelectListItem> selectListItems = new List<SelectListItem>();
foreach (Models.tblDepartmentSelected dep in
{
SelectListItem selectListItem = new SelectListItem
{
Text = dep.Name,
Value = Convert.ToString(dep.Id),
? dep.Selected.Value : false)
};

}
ViewBag.DepartmentsSelected = selectListItems;
return View();
}

gulshansinghmetharu
Автор

I have two tables and I want to populate dropdown. I want, if I select one department name from the dropdown the sub department get populated for selection. is it possible?

meghamisra
Автор

Sir why the below is not working  to set default value? Cann't we use Lymbda exp here???

ViewBag.Departments = new SelectList(db35, "ID", "Name",

watan
Автор

Hi Venkat,
I am a regular follower of your video series, Thanks for doing such awesome job.
In Part 35 of Asp.Net MVC, I have some confusion 1.when we add a column (IsSelected) to table we have to update a row with 1, If multiple rows will be updated by 1. then there could be a problem,
2.In 2nd process can't we put the default text like Select Department.

Regards
Pawan

uniquejha
Автор

Is there is any way to achieve same without changing the structure of table??

vaibhavnag
Автор

Thanks Venkat,
Please Help me. I want, On dropdown selection fill Textbox with database column value.

anandrajput
Автор

Hey, I think this is one of my first comments on youtube.. I just want to say that your videos are excellent. They helped me so much when I was learning ASP.NET. Thanks you so much and best wishes from Austria.

danielruckenbach
Автор

great, but what happens if 2 users use that dropdownlistbox

leviatanMX
Автор

How to do it using dropdownlistfor i am having a need to add class to dropdownlist please tell me how to do that.

arun
Автор

Sir plz make video for salesforce language

raghavmaheshwari
Автор

can u please upload a full length project which includes all the concepts covered in that

hyderabadcity
Автор

and also can u tell me how to get a selected dropdown list item when i have a list of people in the table

hyderabadcity
welcome to shbcf.ru