1

Here I was trying to make an extension with double input boxes

the returned model's properties are null, They are not correctly bound or there is another issue here, please help me on writing this extension and make use of it. I need to use thousands of it during my code.

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);

        // 1st TextBox
        var tagInputLeft = new TagBuilder("input");
        tagInputLeft.Attributes.Add("name", fullName);
        tagInputLeft.Attributes.Add("id", fieldId + 1);
        tagInputLeft.Attributes.Add("type", "text");
        tagInputLeft.Attributes.Add("value", model.Num1 == null ? "" : model.Num1.ToString());

        // 2nd TextBox
        var tagInputRight = new TagBuilder("input");
        tagInputRight.Attributes.Add("name", fullName);
        tagInputRight.Attributes.Add("id", fieldId + 2);
        tagInputRight.Attributes.Add("type", "text");
        tagInputRight.Attributes.Add("value", model.Num2 == null ? "" : model.Num2.ToString());

        builder.Append(tagInputLeft.ToString(TagRenderMode.SelfClosing));
        builder.Append(tagInputLeft.ToString(TagRenderMode.SelfClosing));
        return new MvcHtmlString(builder.ToString());
    }
}

The Model class

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

And Here is the controller -Post action which we need the entered numbers

[HttpPost]
public ActionResult Index(DoubleNumber g)
{
    int iNum1 = g.Num1; // int or string doesn't matter, it couldn't get the entered(modified) model
    int iNum2 = g.Num2;
    var vm = ViewModelOf(g);
    return View(vm);
}
1
  • If anyone can also mention how can I use something similar to this: "htmlHelper.TextBoxFor( expression " inside my string builder which I can target my Model's needed properties through it, it could be a big help. Commented May 19, 2014 at 8:37

1 Answer 1

2

You'd probably have an easier time just using an editor template:

Views\Shared\EditorTemplates\DoubleNumber.cshtml

@model DoubleNumber
@Html.TextBoxFor(m => m.Num1)
@Html.TextBoxFor(m => m.Num2)

Then, in your view:

@Html.EditorForModel()

Or, if you've got a property of type DoubleNumber on a view model:

@Html.EditorFor(m => m.MyDoubleNumberProperty)
Sign up to request clarification or add additional context in comments.

1 Comment

That is OK I think +1, it could be similar to PartialView approach, but what if you are making a re-usable library and you are trying to put some similar logic as a control, Also I'm trying to find what I missed here, please consider the alternative way also, thanks

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.