0

I have a problem when I want to bind a value from my model to a textbox, here is my MVC view code:

@Html.TextBox("SellerBroker", model => model.OutOfMarket.BuyerBroker.Name , new { @class = "control-label" })

I want my textbox to have a name or 'SellerBroker' and it's value to come from my model property model => model.OutOfMarket.BuyerBroker.Name and with HTML attributes of class = "control-label". However, I am receiving the following error:

Cannot convert lambda expression to type 'object' because it is not a delegate type

Luke
  • 22,826
  • 31
  • 110
  • 193
S.Faraji
  • 97
  • 2
  • 13
  • `@Html.TextBox("SellerBroker", Model.OutOfMarket.BuyerBroker.Name , new { @class = "control-label" })` but why would you do this instead of binding to your property - `@Html.TextBoxFor(m => m.OutOfMarket.BuyerBroker.Name , new { @class = "control-label" })`? –  Oct 28 '15 at 08:21

2 Answers2

1

The @Html.TextBox() can be used for generating a textbox with an initial value (one way binding).

If you want to really bind the textbox to your class property (two ways binding), you should use the @Html.TextBoxFor() helper. This method take as parameter a lambda expression, as used in your example.

You can found more details on TextBox helpers at : Html.Textbox VS Html.TextboxFor

Community
  • 1
  • 1
Paul DS
  • 859
  • 7
  • 13
1

Helper @Html.TextBox() does not contain overloard that have lambda parameter. You should use it without lambda like this like @Stephen Muecke advice you:

@Html.TextBox("SellerBroker", Model.OutOfMarket.BuyerBroker.Name , new { @class = "control-label" })

If you want to use lambda you should use @Html.TextBoxFor() helper. But you should change name like this:

@Html.TextBoxFor(model => model.OutOfMarket.BuyerBroker.Name, new { Name = "SellerBroker", @class = "control-label"})
teo van kot
  • 12,350
  • 10
  • 38
  • 70