.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Model Binding with DropDownList

Posted By:      Posted Date: October 15, 2010    Points: 0   Category :ASP.Net

Hello again -

I seem to be having issues with posting forms with DropDownLists. I've looked all over the net and tried various solutions, but none seem to work for me.

I'm getting the following error: The parameter conversion from type 'System.String' to type 'Models.Organization' failed because no type converter can convert between these types.

Here's my ViewModel:

    Public Class RegistrationViewModel
        Public Property User() As User
                Return _user
            End Get
            Set(ByVal value As User)
                _user = value
            End Set
        End Property
        Private _user As User

        Public Property ConfirmPassword() As String
                Return _confirmPassword
            End Get
            Set(ByVal value As String)
                _confirmPassword = value
            End Set
        End Property
        Private _confirmPassword As String

        Public Property Profile() As Profile
                Return _profile
            End Get
            Set(ByVal value As Profile)
                _profile = value
            End Set
        End Property
        Private _profile As Profile

        Public Property PhoneNumber() As PhoneNumber
                Return _phoneNumber

View Complete Post

More Related Resource Links

Binding WPF Controls to an Entity Data Model

In this interview, programming writer, McLean Schofield, demonstrates how to bind WPF controls to an entity data model, using Visual Studio 2010 Beta 1. You can also learn more in the topic: Walkthrough: Binding WPF Controls to an Entity Data Model.

Binding WPF Controls to an Entity Data Model

In this interview, programming writer, McLean Schofield, demonstrates how to bind WPF controls to an entity data model, using Visual Studio 2010 Beta 1. You can also learn more in the topic: Walkthrough: Binding WPF Controls to an Entity Data Model.

Data Binding to Dropdownlist


   private void AddDBDataToControl(DataSet app_Support,int count)
        int i = 0;
        while (count != 0)

            DateTime dt = Convert.ToDateTime(app_Support.Tables[0].Rows[i]["FromDate"].ToString());

//dt=23/08/2010 10:04:00 AM

            DropDownList h1 = (DropDownList)GVDate.Rows[i].Cells[1].FindControl("txthour");
            h1.SelectedValue = dt.Hour.ToString();
            DropDownList m1 = (DropDownList)GVDate.Rows[i].Cells[2].FindControl("txtmin");
            m1.SelectedValue = dt.Minute.ToString();

            DropDownList h2 = (DropDownList)GVDate.Rows[i].Cells[5].FindControl("txthourto");
            DateTime dt2 = Convert.ToDateTime(app_Support.Tables[0].Rows[i]["To

Model Binding the values in an HTML Table back to Controller / Model


Currently, I am working with ASP.NET MVC1 and am still learning about Model Binding and how values from a View are passed back to the Controller / Model. Specifically, I want take an existing Model, create a Table and populate the Rows of the Table, allow the user to edit some fields and pass it back.

In my example, I have a Class called "Ingredient" which has 4 public accessors:  Name, Barcode, Amount, and Unit

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcACE.DomainModel.Entities.Ingredient>>" %>

<% using (Html.BeginForm("SubmitOrder","MyController"))
{ %>

<% if (Model != null)
foreach (var item in Model)
{ %>
<td><%= Html.Encode(item.Name)%></td>
<td><%= Html.Encode(item.Barcode)%></

Why won't my Model accept what I'm selecting from the DropDownList?


I edit the Dinner (from the NerdDinner tutorial) and save it, but the Country isn't persisted. Here's my code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NerdDinner.Models
public class DinnerFormViewModel
private static string[] _countries = new[]{

public Dinner Dinner { get; private set; }
public SelectList Countries { get; private set; }

public DinnerFormViewModel(Dinner dinner)
Dinner = dinner;
Countries = new SelectList(_countries, dinner.Country);

Then in my controller I use it like so:
    public ActionResult Edit(int id)
        Dinner dinner = dinnerRepository.GetDinner(id);        

What is the correct error handling in custom model binding?

Hello,I am working on a Custom Model Binder where a certain error can occur.When this happens it returns null and a error is added to the ModelState.The problem is that in some projects I am using Fluent Validation and the errors messages I am adding through FV are not taking effect.The message added by the Model Binder always prevails.Should the Model Binder add error messages or just bind the field and return null if some problem happened?What would be the correct implementation for this?Thanks,Miguel

Problem Binding DropdownList in a gridView to a datasource

I have a Dropdownlist in an itemTemplate in a gridview. My gridView binds to dataTable1 (from which the selected value for dropdown comes from) and dropDown binds to dataTable2 (i need to get DataValueField and DataTextField from here).<asp:TemplateField HeaderText="Category"> <ItemTemplate> <asp:DropDownList ID="Cat" runat="server" SelectedValue='<%#Bind("CATEGORY") %>' > </asp:DropDownList> </ItemTemplate> <FooterStyle BorderStyle="None" /> <HeaderStyle BorderStyle="None" /> <ItemStyle BorderStyle="None" /> </asp:TemplateField>How do I do that? thanks in advance!

Problem binding data to DropDownList in DetailsView

Hi guys. I have a slight issue I awas trying to fix for 2-3 days, but nothing I try works. The scenario: DetailsView control in edit mode, I pass parameters to the SqlDataSourceProducts (edit.aspx?id=2) and the data source selects the appropriate data and fills in the DetailsView form. Except two drop down lists. These two DDLs (DDL2 and DDL3) are dependent on the previous DDL (DDL1) in the DetailsView. Thanks to PeteNet, when the selected value of the DDL1 changes, DDL2 and 3 change their lists and get bound to the database. The problem is: when I edit something from the database, DDL2 and 3 appear empty and when I update any other field and leave these, they get blanked in the database.  My goal is to set the selected value of DDL2 and 3 to the values in the database WHERE ID = QueryStringField="id". I can go for not binding the data unless the field is changed, but does not sound professional and user friendly. code: The SqlDataSource used to select the parameters from the table and populate the DetailsView <asp:SqlDataSource ID="SqlDataSourceProduct" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT ID, wm_number, upc, check_digit, unit, price_unit, brand, category, shelves, type, cooking, storage, packaging, origin, nutriti

Binding dropdownlist along with Heading


I need to bind a dropdownlist to multiple column along with the column heading.. the heading should be unselectable

something similar to this

Each group are in differnet tables..

How can i implement this

its consuming my time.. thanx in advance...

binding dropdownlist to datasource C#


 I have the following code in which I am trying to use a dropdownlist to update values in a gridview. I having trouble getting the datasource to bind to the dropdownlist

here is my code:

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
                if (e.Row.RowState == DataControlRowState.Edit)
                    DropDownList DropDownList1 = new DropDownList();
                    string Query = "SELECT DISTINCT STATUS FROM VW_MSG_VIEW";
                    DataSet dt = new DataSet();
                    OracleCommand cmd1 = new OracleCommand(Query, con);
                    OracleDataAdapter sqlDa = new OracleDataAdapter(cmd1);
                    DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
                    ddl.DataSource = dt;
                    ddl.DataValueField = "STATUS";
                    ddl.DataTextField = "STATUS";

I am getting nullreference exception on line ddl.DataSource = dt;


Thanks for any help

binding xml to dropdownlist declaratively


i have xml file like this.


<?xml version="1.0" encoding="utf-8" ?>
    <Name>My State 1</Name>
    <Name>My State 2</Name>
      <Name>My State 3</Name>
      <Name>My State 4</Name>
      <Name>My State 5</Name>

and an aspx file


<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <form id="form1" runat="server">

Problem binding a collection to a view model on POST when manipulating the DOM with javascript.


Hi, i'm currently working on a strongly typed view and i'm binding a collection to a table like this:


for (int i = 0; i < Model.Cars.Count; i++)





<%= Html.Hidden(String.Format("Cars[{0}].Id", i),




<%= HtmlTextBox(String.Format("Cars[{0}].CarModel", i),



Fowarding Model Binding



Is it possible to forward model binding and if so, how? Example is:

public ActionResult SystemWideCatcher(FormCollection Values)


//all requests go thru here, some internal logic to decide where to go...

//Somehow bind model here??

ExternalDll objExternal = new ExternalDll();

return objExternal.DoWork(MAGIC_BOUND_MODEL);



public ActionResult DoWork(Person p)


//doing work...



How to access property values in the model that you are binding to? - Help!


From within a custom modelBinder (derived from DefaultModelBinder), how do I access the existing values of properties in the model into which the new values are being injected?

Thanks in advance for your help!

DropDownList Binding - Ways to Increase Speed?


I have a gridview that displays query results on a reporting page for a "home-grown" CRM application. From the gridview, a link can be clicked which opens a modal window that contains an iframe linked to a "details" page where record specific info can be viewed/altered. On the details page there are 10 or so dropdownlists (ddls) that are bound every time the page is called. The ddls are bound using a method similar to the example below. I am looking for a way top speed things up a bit. The details page could be called 10-20 times in one session and every time the page loads these ddls have to be bound from the same methods but for different records. What are some options for speeding up the load time for this page??

if (!Page.IsPostBack)
     //call initial processes
     //and a few more method calls for binding ddls....

private void GetModel()
        string connString = ConfigurationManager.ConnectionStrings["processes"].ConnectionString;
        string cmdString = "SELECT Model, Type + '§' + CAST(ID AS NVARCHAR) + '§' + Factory AS Vals FROM Machines WHERE (Active = 'True')" +
                           "ORDER BY Mod

Help: populate Html.DropDownList from table data? [Linq to SQL model]


I have two tables, Users and Roles.  The roles table is a reference table for the different roles of my site (RoleId and RoleName columns). The Users table contains profile data for each user, including a RoleId column that is FK'd back to the Roles table [RoleId].

Both tables are in a Linq to SQL model.

I have an edit page created which lists all the profile values of a user.  The edit page inherits the User model.  One of the editable values is the RoleId field.  I want to set this up to be a drop-down list that contains items labeled using the RoleName column of the Roles table, and matched with values of RoleId.

How can I achieve this?

Model Binding - Byte[] field


I have a class that has a byte[]  type field in it. When I create a strongly-typed view for this class, this byte[] type field is missing from the generated rendering. I am using MVC2 VS2010. Is this a bug ?

Secondly, what is the best way to render it as a Hidden field ?





ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend