Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModelMetadata.Watermark and MVC View Models

I'm struggling to display a watermark (so called placeholder) on my MVC3 form inputs.

There is already few posts down here talking including the quite focused one here:

Html5 Placeholders with .NET MVC 3 Razor EditorFor extension?

In these posts, advice is made to create tweaked html.TextBox templates. In my case, my asset is that I should not need any editor template as I'm tweaking them inline.

Better than long talks, here is the relevant part of the actual code:

~/Models/myModel.cs

namespace myProject.Models {
  public class myFormModel {
    ...
    [Display(Name = "firstFieldName", Prompt = "firstFieldPrompt")]
    public string firstFieldValue { get; set; }
    ...
  }
}

~/Controllers/myFormSurfaceController.cs

namespace myProject.Controllers {
  public class myFormSurfaceController : SurfaceController {
    ...
    [ChildActionOnly]
    public PartialViewResult myForm()
    {
        return PartialView("myPartialView", new myFormModel());
    }
    ...
    [HttpPost, ValidateAntiForgeryToken]
    public ActionResult handleMyFormSubmit(myFormModel model) {...}
    ...
  }
}

~/Views/myProject/Partial/myPartialView.cshtml

@model myFormModel
@{      
  using (Html.BeginUmbracoForm("handleMyFormSubmit", "myFormSurface", null, new Dictionary<string, object> { { "class", "myFormStyle" }, { "id", "myFormId" } }))
  {
    ...
    @Html.TextBoxFor(x => x.firstFieldValue, new { @class = "myInputStyle", @placeholder = ViewData.ModelMetadata.Watermark })
    ...
  }
}

Result is that the placeholder html tag is showing up correctly on my rendered webpage but is empty though Name tag is filled up correctly, even without DisplayName decoration set on my view model's property.

http://localhost/testpage

...
<input type="text" value="" placeholder="" name="firstFieldName" id="firstFieldName" class="myInputStyle">
...

What am I missing here ? I did try indeed to create both editor templates (MultilineText and String) in the correct folder (~/Views/Shared/EditorTemplates/) but I assume they are never called as I'm using "Html.TextBoxFor" and not "Html.TextBox"...

Other thing, if I remove "@placeholder = ViewData.ModelMetadata.Watermark" from the @Html.TextBoxFor call, I don't have any "placeholder" displayed on the rendered webpage. Which is good, this part of the call is definitively fine.

Thanks in advance for any help on that point...

Nicolas.

Edit:

What about if I create more variable in my model.

For instance:

public string firstFieldPrompt { get { return "bla"; } set { } } 

and then

@Html.TextBoxFor(x => x.firstFieldValue, new { @class = "myInputStyle", @placeholder = x => x.FirstFieldPrompt })

?

like image 443
user1288337 Avatar asked Mar 23 '12 14:03

user1288337


1 Answers

I realise this is an oldie, but you can use the ModelMetadata.FromLambdaExpression() method from within your view (without using templates), i.e.

    @Html.TextBoxFor(x => x.firstFieldValue, 
        new { 
            @class = "myInputStyle", 
            @placeholder = ModelMetadata.FromLambdaExpression(x => x.firstFieldValue, ViewData).Watermark 
    })

Hope this helps someone :-)

like image 163
Kristine Avatar answered Sep 23 '22 19:09

Kristine