0

The code I'm currently using is this:

@Html.LabelFor(model => model.MaxExecutions)
<div class="input-append">
    @Html.TextBoxFor(model => model.MaxExecutions, new { @class = "input-xxlarge", placeholder="Enter Max Executions" })
    <span class="add-on"><a href='#' class='title' rel='tooltip' title="Help Title" data-content="Help Message">?</a></span>
    @Html.ValidationMessageFor(model => model.MaxExecutions)
</div>

I repeat this over & over in my code. I'd love to pass this off to an HtmlHelper. The type of Model I pass in can change, and I'd like to have the Display attribute available from the model (or I'd just generate an htmlstring).

1 Answer 1

1

You can implement an extension method or a helper (static) class but there's times where I like the declarative version (CSHTML). Either way works and can be passed around from controller, etc.

~/App_Code/Blah.cshtml

@helper ExecuteHelper() {
   @Html.LabelFor(model => model.MaxExecutions)
   <div class="input-append">
       @Html.TextBoxFor(model => model.MaxExecutions, new { @class = "input-xxlarge",    placeholder="Enter Max Executions" })
    <span class="add-on"><a href='#' class='title' rel='tooltip' title="Help Title" data-content="Help Message">?</a></span>
   @Html.ValidationMessageFor(model => model.MaxExecutions)
   </div>
}

Then call it in view like:

@Blah.ExecuteHelper()

I've noticed the amount of HTML you have it in which is why I opted for the @helper syntax solution. Here's an article by Scott Guthrie that outlines this MVC feature: Article

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

2 Comments

Definitely didn't know you could arbitrarily define & execute code like that. Certainly a new tool to add to my belt. I'm still not sure how I'd "genericize" this to use any model type & associated property, though... any thoughts?
Sorry for the late response but you can try adding the model as a parameter that will be used inside the helper. It would be @Blah.ExecuteHelper(model)

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.