Home > ASP.NET MVC, dotnet 4, JQuery > HTML Atrributes in MVC 2

HTML Atrributes in MVC 2

Recently I was working with a very interesting form problem and MVC 2, anyway I manage to solve the problem and wanted to use jQuery validation plugin. I knew i had use the htmlattributes property to inject the attributes and this is how it is done.


<div class="editor-field">

    <%: Html.TextBoxFor(model => model.Date, new {size = 25}) %>

    <%: Html.ValidationMessageFor(model => model.Date)%>



So here we are saying to the MVC View engine is that when the textbox for the Date field is generated add size as an html attribute with a value of 25,and this is how the output looks in Firebug.

So far so good and now I wanted to use JQuery Validation plugin to validate the date textbox before I submit the form. This is how JQuery validation plugin does a automatic validation of a form.

This is direct code from the Jquery validation plugin page which is at http://docs.jquery.com/Plugins/validation

However when i tried using the class reserved as a htmlattribute I got a clueless runtime error as “CS1513: } expected” and after some mucking around I came to know that you have to use @class instead of class . So when i change my code to this ..

<div class="editor-field">

    <%: Html.TextBoxFor(model => model.Date, new {@class = "required"}) %>

    <%: Html.ValidationMessageFor(model => model.Date)%>


and as you can see we got what we wanted:)

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: