AlwaysLearning
AlwaysLearning

Reputation: 2875

Using jQuery Functions in MVC3 with model

I'm trying out jQuery in an MVC3 ASP.NET app but I'm struggling with the model aspects.

This is not working:

I've declared a TextBox Watermark library:

<script src="../../Scripts/jquery.tinywatermark-3.1.0.js" type="text/javascript"></script>

I then add my jQuery:

<script type="text/javascript">
    $(function () {
        $('.watermarked').watermark('watermark', 'Please Enter...');
    });
</script>

Then add the watermarked class to my div model field:

<div class="editor-field">
     @Html.EditorFor(model => model.SchemeName, new { @class = "watermarked" }) 
    @*<input class="watermarked"  @Html.EditorFor(model => model.SchemeName) />*@
    @Html.ValidationMessageFor(model => model.SchemeName)

If I uncomment the section it sort of works but I want to maintain the Razor model, any ideas, anyone?

Upvotes: 2

Views: 360

Answers (2)

CD Smith
CD Smith

Reputation: 6607

I'm not sure what the preference for EditorFor() is that you need it but this should work just fine

@Html.TextBoxFor(model => model.SchemeName, new { @class = "watermarked" })

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039298

The following line:

@Html.EditorFor(model => model.SchemeName, new { @class = "watermarked" })

doesn't do what you think it does. The second argument that you are passing to the EditorFor helper doesn't do what you think it does. It sends additional view data to the custom editor template. It doesn't apply any html attributes as you might be thinking. If you want this syntax to work you have the possibility to write a custom metadata provider and a custom editor template as illustrated in the following article.

Upvotes: 3

Related Questions