3

I'm trying to create a helper that will make my form controls in the whole website

Here is what i use till now

@helper editorField(System.Linq.Expressions.Expression<Func<Model, String>> o)
{
    <div class="form-group">
        @Html.LabelFor(o, new { @class = "col-md-4 control-label" })
        @Html.ValidationMessageFor(o, "", new { @class = "col-md-6 col-xs-12  text-errer" })
        <div class="col-md-5">
            @Html.TextBoxFor(o, new { @class = "form-control" })
        </div>
    </div>
}

And the way i use it is

        @editorField(model => model.Name)
        @editorField(model => model.Email)
        @editorField(model => model.PhoneNumber)

this make it really easy to change the style and layout of the whole website in 1 place

Now here is the problem

I need to make helper for each Model and data type

Im looking for a way to make it work something like this

@helper editorField(object o)
{
        <div class="form-group">
            @Html.LabelFor(o, new { @class = "col-md-4 control-label" })
            @Html.ValidationMessageFor(o, "", new { @class = "col-md-6 col-xs-12  text-errer" })
            <div class="col-md-5">
                @Html.TextBoxFor(o, new { @class = "form-control" })
            </div>
        </div>
}

and it should work for all Models and datatypes

4
  • 2
    Have a look at this: stackoverflow.com/questions/15968305/… Commented Apr 2, 2015 at 18:07
  • 2
    Here are my thoughts on this question: Something like this would be best defined as an Html helper extension (or set of extensions) in a separate code file. You would have to implement this for each type you want to allow, or you would have to take an object, figure out its type, and cast it. I would just implement it for each type. What more do you need than int and string? Commented Apr 2, 2015 at 18:45
  • i need int, string, decimal , and ,double Commented Apr 2, 2015 at 18:52
  • 1
    Refer this example for creating a custom helper Commented Apr 2, 2015 at 23:15

1 Answer 1

0

So basically what you are trying to achieve is a generic HTML helper. Unfortunately those are not supported directly in Razor views. But, you can use this workaround:

@functions
{
    public HelperResult PasteEditorFor<TModel, TItem>(HtmlHelper<TModel> html, Expression<Func<TModel, TItem>> expr)
    {
        return editorField(
            html.LabelFor(expr, new { @class = "col-md-4 control-label" }), 
            html.ValidationMessageFor(expr, "", new { @class = "col-md-6 col-xs-12  text-errer" }), 
            html.TextBoxFor(expr, new { @class = "form-control" }));
    }
}

@helper editorField(MvcHtmlString label, MvcHtmlString validationMessage, MvcHtmlString textBox)
{
     <div class="form-group">
         @label
         @validationMessage
         <div class="col-md-5">
            @textBox
         </div>
     </div>
 }

// usage sample
@PasteEditorFor(Html, m => m.LongDescription)

But as it is still not easy reusable (can't tell at the moment how to make it visible though all views), so I would recommend you to write usual HtmlHelper with usual static class syntax ( look at Alundra the dreamwalker comment) and add it's namespace to list of default one in WebConfig.

Sign up to request clarification or add additional context in comments.

3 Comments

to make it visible to all views i put it in a app_code folder in a myHelpers.cshtml file and then i call it myHelpers.editorField(...)
how do i call the helper do i need t pass in a html helper as a parameter?
ow yeah i missed it , the issue is that i really don't want to use @functions in my code since i feel it should be in a .cs file and that what im going to do, and as im typing it i see that i get already 2 comments about it stating the same so will go for it. thanks alot

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.