1

I am trying to write an html helper extension within the asp.net mvc framework.

public static MvcHtmlString PlatformNumericTextBoxFor<TModel>(this HtmlHelper instance, TModel model, Expression<Func<TModel,double>> selector)
        where TModel : TableServiceEntity
    {
        var viewModel = new PlatformNumericTextBox();

        var func = selector.Compile(); 

        MemberExpression memExpession = (MemberExpression)selector.Body;
        string name = memExpession.Member.Name;

        var message = instance.ValidationMessageFor<TModel, double>(selector);

        viewModel.name = name;
        viewModel.value = func(model);
        viewModel.validationMessage = String.Empty;

        var result = instance.Partial(typeof(PlatformNumericTextBox).Name, viewModel);

        return result;

    }

The line

var message = instance.ValidationMessageFor<TModel, double>(selector);

has a syntax error. But I do not understand it. The error is: Fehler 2 "System.Web.Mvc.HtmlHelper" enthält keine Definition für "ValidationMessageFor", und die Überladung der optimalen Erweiterungsmethode "System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)" weist einige ungültige Argumente auf. C:\Projects\WorkstreamPlatform\WorkstreamPlatform_WebRole\Extensions\PlatformHtmlHelpersExtensions.cs 97 27 WorkstreamPlatform_WebRole

So according to the message, the parameter is invalid. But the method is actually declared like this:

    public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression);

So actually it should work.

1
  • 1
    It would help if you could translate the exception message :) Commented May 11, 2010 at 14:06

1 Answer 1

4

Change your method declaration to:

public static MvcHtmlString PlatformNumericTextBoxFor<TModel>(
    this HtmlHelper<TModel> instance, 
    TModel model, 
    Expression<Func<TModel,double>> selector) where TModel : TableServiceEntity
{

}

Notice the generic this HtmlHelper<TModel>. Also the second argument is not necessary as you can retrieve the model from the strongly typed helper:

var model = instance.ViewData.Model;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.