I wanted to know if you could set attributes of html controls when creating them in MVC for example take the following code:
<body onload="SetUpRSVP_UI()">
@using (Html.BeginForm())
{
@Html.ValidationSummary()
<div id="divRSVP" class="fLeft">
<p class="TextBox fRight">Your Name: @Html.TextBoxFor(x => x.Name)</p>
<p class="TextBox fRight">Your Email: @Html.TextBoxFor(x => x.Email)</p>
<p class="TextBox fRight">Your Phone: @Html.TextBoxFor(x => x.Phone)</p>
<p id="ddlWillYouAttend" class="TextBox fRight">
Will you attend?
@Html.DropDownListFor(x => x.WillAttend, new[]
{
new SelectListItem() {Text = "Yes, I will be there", Value= bool.TrueString},
new SelectListItem() {Text = "No I can't come", Value = bool.FalseString}
},"Choose an option")
</p>
<input id="RSVPSubmit" class="Button fRight" type="submit" title="fffff" value="Submit RSVP"/>
</div>
}
</body>
If I wanted to give to the drop down list WillAttend a title attribute could I do this at this stage? I can easily writer some jQuery on the OnLoad event of the page but I wondered is this possible any other way?
The last parameter for HTML Helper methods is typically a set of HTML attributes that you can pass in (using an anonymous constructor) , like the following :
@Html.DropDownListFor(x => x.WillAttend, new[]
{
new SelectListItem() {Text = "Yes, I will be there", Value= bool.TrueString},
new SelectListItem() {Text = "No I can't come", Value = bool.FalseString}
},"Choose an option", new { @title="Your Title Attribute Here"})
An additional example setting class, title and additional style fields might look like this :
If only MS supplied some sort documentation you could read to find out. What do you know, they do: http://msdn.microsoft.com/en-us/library/ee834953(v=vs.108).aspx
bruce (sqlwork.com)
Marked as answer by bigredhf on Jan 27, 2013 07:39 AM
bigredhf
Member
48 Points
81 Posts
Adding attributes to html controls as they are created in MVC
Jan 26, 2013 04:40 PM|LINK
Hi All,
I wanted to know if you could set attributes of html controls when creating them in MVC for example take the following code:
<body onload="SetUpRSVP_UI()"> @using (Html.BeginForm()) { @Html.ValidationSummary() <div id="divRSVP" class="fLeft"> <p class="TextBox fRight">Your Name: @Html.TextBoxFor(x => x.Name)</p> <p class="TextBox fRight">Your Email: @Html.TextBoxFor(x => x.Email)</p> <p class="TextBox fRight">Your Phone: @Html.TextBoxFor(x => x.Phone)</p> <p id="ddlWillYouAttend" class="TextBox fRight"> Will you attend? @Html.DropDownListFor(x => x.WillAttend, new[] { new SelectListItem() {Text = "Yes, I will be there", Value= bool.TrueString}, new SelectListItem() {Text = "No I can't come", Value = bool.FalseString} },"Choose an option") </p> <input id="RSVPSubmit" class="Button fRight" type="submit" title="fffff" value="Submit RSVP"/> </div> } </body>If I wanted to give to the drop down list WillAttend a title attribute could I do this at this stage? I can easily writer some jQuery on the OnLoad event of the page but I wondered is this possible any other way?
Thanks. DJ.
Rion William...
All-Star
26445 Points
4389 Posts
Re: Adding attributes to html controls as they are created in MVC
Jan 26, 2013 04:53 PM|LINK
The last parameter for HTML Helper methods is typically a set of HTML attributes that you can pass in (using an anonymous constructor) , like the following :
@Html.DropDownListFor(x => x.WillAttend, new[] { new SelectListItem() {Text = "Yes, I will be there", Value= bool.TrueString}, new SelectListItem() {Text = "No I can't come", Value = bool.FalseString} },"Choose an option", new { @title="Your Title Attribute Here"})An additional example setting class, title and additional style fields might look like this :
new { @class = "Your Class", @title = "Your Title", style = "Your Style Attributes Here" }bruce (sqlwo...
All-Star
36644 Points
5432 Posts
Re: Adding attributes to html controls as they are created in MVC
Jan 26, 2013 09:45 PM|LINK
bigredhf
Member
48 Points
81 Posts
Re: Adding attributes to html controls as they are created in MVC
Jan 27, 2013 07:40 AM|LINK
Thanks for the Rion that is brilliant
bigredhf
Member
48 Points
81 Posts
Re: Adding attributes to html controls as they are created in MVC
Jan 27, 2013 07:40 AM|LINK
Cheers Bruce I appreciate you help