Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
This is My Model

C#
public class InsertOrder
    {
        public int CustomerId { get; set; }
        public string CustomerName { get; set; }     
       } 
        public List<insertorder> getcustomerid()
        {
            List<insertorder> customerlist = new List<insertorder>();
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString);
            SqlDataReader Datareader;
            {
                using (SqlCommand command = new SqlCommand("SELECT CustomerID,FirstName FROM customers", connection))
                {
                    connection.Open();
                    Datareader = command.ExecuteReader();
                    while (Datareader.Read())
                    {
                        InsertOrder ID = new InsertOrder();
                        ID.CustomerId = Convert.ToInt32(Datareader["CustomerID"]);
                        ID.CustomerName = Datareader["FirstName"].ToString();

                        customerlist.Add(ID);
                    }
                    Datareader.Close();
                    return customerlist;
                }
            }
        }
    }
}


how to give in the view page
I want to bind it to a dropdownlist
Posted
Updated 13-Nov-14 6:13am
v3

You can do it like so:

This is the InsertOrder class:
C#
public class InsertOrder
{
    public int CustomerId { get; set; }
    public string CustomerName { get; set; }

    public List<SelectListItem> SelectSource { get; set; }

    public InsertOrder()
    {
        SelectSource = new List<SelectListItem>();

        using (
            var connection =
                new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString))
        {
            using (var command = new SqlCommand("SELECT CustomerID,FirstName FROM customers", connection))
            {
                connection.Open();
                using (var dataReader = command.ExecuteReader())
                {
                    while (dataReader.Read())
                    {
                        var ID = new SelectListItem
                        {
                            Value = dataReader["CustomerID"].ToString(),
                            Text = dataReader["FirstName"].ToString()
                        };

                        SelectSource.Add(ID);
                    }
                }

            }
        }
    }
}


C#
@Html.DropDownFor(model => model.CustomerId, Model.SelectSource)


Now since you have a customer Id, you're better off NOT having the customer name field in this model, but if you just can't part with it, you can pull the name out via jQuery:

JavaScript
<script type="text/javascript">
$('#CustomerId').select(function(){ 
    $('#CustomerName').val($('#CustomerId option:selected').text());
});
</script>
 
Share this answer
 
v2
Comments
Member 11166907 13-Nov-14 8:19am    
Where should i add the javascript @NathanMinier
Nathan Minier 13-Nov-14 8:37am    
Wherever. If you don't currently have a document.ready() block, do it at the top of the body and modify it like so:

<script type="text/javascript">
$(function(){
$('#CustomerId').select(function(){
$('#CustomerName').val($('#CustomerId option:selected').text());
});
});
</script>
Member 11166907 13-Nov-14 8:49am    
In view page @Html.DropDownFor(model => model.CustomerId, Model.getcustomerid())
It is showing can't convert a lamda expression to type string because it is no longer a delegate type
Nathan Minier 13-Nov-14 9:42am    
I found the issue, it was the:
ID.CustomerId = Convert.ToInt32(Datareader["CustomerID"]);

At any rate, I've updated the solution with a version that won't be leaky.
Member 11166907 13-Nov-14 10:03am    
Its Showing


Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 16: <tr>
Line 17: <td>
Line 18: @Html.DropDownListFor(model => model.CustomerId, Model.SelectSource)
Line 19:
Line 20: </td>

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