Reputation: 4533
I have a form in an MVC view which contains a number of text boxes, drop down lists and text areas. I am using the HTML helper to create these controls, including pre-populating them with View Data where appropriate, and applying styles via the htmlAttributes parameter.
This is working fine with TextBox controls and DropDownLists etc, however when I add the htmlAttributes to the TextArea it stops working, claiming that the best overloaded method has some invalid arguments, the code that is failing is:
Html.TextArea("Description", ViewData["Description_Current"], new { @class = "DataEntryStd_TextArea" })
The resulting error is:
'System.Web.Mvc.HtmlHelper' does not contain a definition for 'TextArea' and the best extension method overload 'System.Web.Mvc.Html.TextAreaExtensions.TextArea(System.Web.Mvc.HtmlHelper, string, string, object)' has some invalid arguments
For comparison the TextBox calls that are working fine are:
Html.TextBox("TelephoneNumberAlternate", ViewData["TelephoneNumberAlternate"], new { @class = "DataEntryStd_TextBox" })
I tried explicitly referencing the TextAreaExtensions.TextArea and including the HtmlHelper argument however this made no difference.
For info the TextArea call works fine without the htmlAttributes parameter. Additionally I have tried specifying a name/value dictionary for the class attribute however this suffers the exact same problem.
Any ideas what I'm doing wrong?
Upvotes: 4
Views: 10518
Reputation: 532755
You need to cast the ViewData["Description_Current"] to string as the method requires the signature (string, string, object) not (string, object, object). The TextBox works because there is a signature using html attributes that takes (string, object, object).
<%= Html.TextArea( "Name",
(string)ViewData["Value"],
new { @class = "klass" } ) %>
Docs for HtmlHelper.TextBox and HtmlHelper.TextArea are available at MSDN.
Upvotes: 3
Reputation: 60674
It always bugs me that these error messages don't tell you which one of the arguments don't match.
Have you tried this?
Html.TextArea("Description", ViewData["Description_Current"].ToString(), new { @class = "DataEntryStd_TextArea" })
The reason I ask is that the ViewData["Description_Current"]
is of type Object
, and there is an overload with the signature Html.TextArea(String, Object)
- although the object in this case represents the html attributes. That could be why the compiler doesn't complain until you add the html attributes as a third parameter - until then, the second parameter is allowed to be an Object
, but as soon as you add the third parameter the second has to be a String
.
Upvotes: 12