1

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

13
  • 1
    Well where is your DoubleNumber type defined? Does it have a Num1 property? Commented May 19, 2014 at 10:26
  • looks as though it is a method which takes in a parameter of type DoubleNumber and returns Num1 Commented May 19, 2014 at 10:27
  • Yes, It's inside the given link. a simple class with Num1 and Num2 integer properties Commented May 19, 2014 at 10:28
  • I'm not good at defining expressions, so maybe the structure or passed items should change, I try to edit and paste some pieces from the previous question I had, which will show some other parts of the code. Commented May 19, 2014 at 10:30
  • 1
    Shouldn't that be Expression<Func<DoubleNumber,int>> expr2 = (DoubleNumber g) => g.Num1;? Commented May 19, 2014 at 10:36

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

9 Comments

The most important part here to me is that the extension as you said will target a specific type which is going to show that in a proper way, If you could mention how instead of "expression" in your code I can use "Num1" property of that type, everything is done and that's fine, thanks for your help.
I can satisfy of it to be generic, but please move your program-side assignment into the extension-side itself, which I could assign each property for example into a different textbox or etc... , thanks +1
Thanks for all the help and time, whenever you came back please let me know how can I access to my model's property instead of the whole "expression" inside that extension by any possible way.
@LastBye I'm not sure I understand exactly what you want, but I added some info to the answer.
Yes I want to be more specific than generic, the problem here is that we miss the model which I want to get it back from the texboxes.
|

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.