Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to MVC application and I have good experience in web forms. please give some examples to bind dropdown list using Models and Stored procedure which means using ADO. Net in MVC.
I have created Employee project same as from the below URL.

C# Corner[^]

I would like to add Person Title such as Mr, Mrs, etc. and
City Name such as Chennai, Bangalore, etc.
Please help me out.
Posted
Updated 17-Mar-16 13:34pm
v3

1 solution

You can do this with the help of Viewbag. Let us say you have the different titles stored in a database table called TitleTable.

Use ADO.Net to get all the titles from TitleTable and create a list of SelectListItem like:(assuming you know how to connect to DB and get data)
C#
titleList = (from titleRow in dtTitle.AsEnumerable()
select new SelectListItem()
{
Selected = false,
Text = resRow["Title"].ToString(),
Value = resRow["Title"].ToString()
}).ToList();

Above code uses datatable, but you can use dataset or datareader.
In order to access this titleList from create view, put this into a ViewBag. This has to be done before the user is sent to the create view (before return View() statement in the Create controller action method).
C#
ViewBag.Titles = titleList;

Now you can use this ViewBag in your view to populate the dropdown list. Before that add a new property to the model you have for Person, like:
C#
public string PersonTitle {get; set;}

And in the view add control for Title and populate it with ViewBag. Here is the sample code:
<div class="form-group">
  @Html.LabelFor(model => model.Year, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
      @Html.DropDownListFor(model => model.PersonTitle, new                        SelectList(@ViewBag.Titles, "Value", "Text"), "--Select Title--")
      @Html.ValidationMessageFor(model => model.Titles)
    </div>
</div>

Hope this helps.
 
Share this answer
 
Comments
Member 9018012 18-Mar-16 4:59am    
Please explain about DDL Edit/update..
how to set Autopostback=true in MVC.
Mathi Mani 18-Mar-16 12:56pm    
There is no postback in mvc. Whenever the user submits the form after filling the details, the controller action method receives the values entered by the user and Title will be a part of it. Or you can also use jQuery to get the selected value as mentioned in comments by Rishikesh.
Rishikesh_Singh 18-Mar-16 10:59am    
there is nothing like autopostback in mvc like webforms. You have to do something like below
@Html.DropDownListFor(model => model.PersonTitle, new SelectList(@ViewBag.Titles, "Value", "Text"),
new { onchange = @"
var form = document.forms[0];
form.action='<methodnameyouwanttocall>';
form.submit();"
} ) %>

OR make a ajax call like

$('ddl').change(function() {
$.ajax({
url: 'yoururl'
.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900