質問

I have an asp.net mvc4 application, in which i have this view :

 @Html.DropDownList("chef",new SelectList(Model,"Id_user","DName"+ "  " +"DFirstName" ))

My Model is a list of objects User

public class User
{
    public int Id_user
    {
        get;
        set;
    }
    public string DFirstName
    {
        get;
        set;
    }
    public string DName
    {
        get;
        set;
    }
}

I need to display two fields DName and DFirstName in the selection items, i get this error

System.Web.HttpException: DataBinding : 'Projet.Models.Administration.User' does not contain a property called 'DName  DFirstName'.

When i try to display a unique field it work.

  1. What is the reason of this problem?
  2. How can i fix it?
役に立ちましたか?

解決 2

The reason you get an exception is that the Html helper method uses reflection to try and find a property with the name "DName DFirstName" on its binding source (your User class) and this property doesn't exist.

Instead create a new property called say FullName

get { return DName + " " + DFirstName; }

Then use this as the display member.

他のヒント

You need to change your model to expose a property that has this get:

get { return string.Format("{0} {1}", DName, DFirstName);

and then bind to that new property instead.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top