0

I would like to rewrite this piece of code using the solution presented here: Html.TextBoxFor formatting or Html.EditorFor htmlAttributes?

<div class="input">
    @Html.TextBoxFor(x => x.Name)
    @MyFunc()
    @Html.ValidationMessageFor(x => x.Name)
</div>
<br />

But, since the call to this template format has no parameters, how can I pass the lambda expression x => x.Name to @Html.ValidationMessageFor?

I have this parameter on the calling page and I need to send it to the EditorFor template page:

Calling page

@Html.EditorFor(m => m.Name)

Classical example EditorFor template page

@model System.DateTime? 
@Html.TextBox("", Model.HasValue ? Model.Value.ToString("dd/MM/yyyy") : string.Empty, new { @class = "date-picker" })

Real example of EditorFor for this case???

<div class="input">
    @Html.TextBoxFor(????????)
    @MyFunc()
    @Html.ValidationMessageFor(??????)
</div>
<br />
Community
  • 1
  • 1
Revious
  • 7,816
  • 31
  • 98
  • 147
  • which version of mvc are you using? – faby Dec 05 '14 at 11:03
  • 1
    Do you mean you want to use `@Html.EditorFor(m => m.DateModified)` and have an `EditorTemplate` include `@Html.ValidationMessageFor()`? In which case `@Html.ValidationMessageFor(m => m)` or `@Html.ValidationMessage("")` –  Dec 05 '14 at 11:25
  • I can't understand what you are trying to achieve. Can you explain me in a better way it? – faby Dec 05 '14 at 11:51
  • When you use EditorFor you can instruct him to use a specific template through the instruction UIHint. In this template I would like to put the code: @Html.ValidationMessageFor(...); can I achieve that objective? – Revious Dec 05 '14 at 12:06
  • @StephenMuecke: yes. I've also clarified the question. – Revious Dec 05 '14 at 12:06
  • 1
    @Revious, Either one of the 2 options I included above will work (and you will might want to include `@Html.Label("")` –  Dec 05 '14 at 12:10
  • @StephenMuecke: thanks! (if you answer I can accept) – Revious Dec 05 '14 at 12:31

1 Answers1

1

You can use @Html.ValidationMessage("") or @Html.ValidationMessageFor(m => m). For example

<div class="input">
    @Html.Label("")
    @Html.TextBox("")
    @Html.ValidationMessage("")
</div>