LastBye
LastBye

Reputation: 1173

MVC Extension - Binding a modified Lambda expression

I'm trying to create a lambda expression to pass to the view via my MVC helper extension to be used.

What I tried is similar to this :

Expression<Func<DoubleNumber,DoubleNumber>> expr2 = (DoubleNumber g) => g.Num1; 

The default expression was passed like this :

    public static MvcHtmlString DoubleBoxFor<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        //int id, 
        Expression<Func<TModel, TProperty>> expression)
    {
...

The Model class

public class DoubleNumber
{
    public int Num1 { get; set; } // tried strings but again they are null
    public int Num2 { get; set; }
}

After using the correct expression format still won't be able to use it : The correct format:

Expression<Func<DoubleNumber,int>> expr2 = (DoubleNumber g) => g.Num1;  

which mentioned in the comments.

Error:

"the type arguments cannot be inferred from the usage. try specifying the type arguments explicitly."

Thanks for any help. Edited 2 - reformed

The Code is proveided :

public static class DoubleBoxHelper
{
    public static MvcHtmlString DoubleBoxFor<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        //int id, 
        Expression<Func<TModel, TProperty>> expression)
    {
        var builder = new StringBuilder();
        var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        var model = metadata.Model as DoubleNumber;
        var name = ExpressionHelper.GetExpressionText(expression);
        var fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
        var fieldId = TagBuilder.CreateSanitizedId(fullName);

// I'm going to use something like this instead of "expression" which seems because of the format or parameter or anything else it's producing an error which I mentioned,

    Expression<Func<DoubleNumber,int>> expr2 = (DoubleNumber g) => g.Num1; 

//The line which will cause the error happened

    builder.AppendLine(htmlHelper.TextBoxFor(expr2 , new {class, etc} )

...
}

I also tried some other forms of TextBoxFor like TextBoxFor or tried to make Expression> each will have some inner issue, and still looking for a neat solution.

Thanks

Upvotes: 1

Views: 459

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 180867

I can't quite seem to repeat your problem, this would seem to compile just fine;

public static class DoubleBoxHelper
{
    public static MvcHtmlString DoubleBoxFor<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TProperty>> expression)
    {
        var builder = new StringBuilder();

        builder.AppendLine(htmlHelper.TextBoxFor(expression, new {bop = 1}).ToHtmlString());
        return null;
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            HtmlHelper<DoubleNumber> helper = ...
            helper.DoubleBoxFor(g => g.Num1);
        }
    }
}

The only problem I can see in your code is that your expression is hard coded to a particular type, while your HtmlHelper is generic. Since the method can be called with any type of HtmlHelper, and the generic parameters need to match between the expression and the helper, the compiler won't let you compile the code. If you either take a hard coded HtmlHelper<DoubleNumber> or as the code above take an expression with matching generic parameters, things compile well.

EDIT: If you want to build the expressions inside the helper, you don't need it to be generic at all;

public static MvcHtmlString DoubleBoxFor(this HtmlHelper<DoubleNumber> htmlHelper)
{
    var builder = new StringBuilder();

    builder.AppendLine(htmlHelper.TextBoxFor(g => g.Num1, new { bop = 1 }).ToHtmlString());
    builder.AppendLine(htmlHelper.TextBoxFor(g => g.Num2, new { bop = 1 }).ToHtmlString());
    return null;
}

Upvotes: 2

Related Questions