Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I apply styling to asp.net mvc @Html.TextboxFor?

I want to change the background of the textbox. This is my code:

@Html.TextBoxFor(p => p.Publishers[0].pub_name) 

What more do I need to write in TextBoxFor to change the background?

like image 771
TCM Avatar asked Jul 31 '11 01:07

TCM


People also ask

What is the difference between HTML textbox and HTML TextBoxFor using ASP.NET MVC Razor engine?

IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.

What is TextBoxFor in MVC?

TextBoxFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, Object) Returns a text input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.

What is the difference between using HTML TextBoxFor and HTML textbox?

Both of them provide the same HTML output, “HTML. TextBoxFor” is strongly typed while “HTML. TextBox” isn't. Below is a simple HTML code which just creates a simple textbox with “CustomerCode” as name.


2 Answers

An overload of the TextBoxFor method allows you to pass an object for the HTML attributes.

@Html.TextBoxFor(p => p.Publishers[0].pub_name, new { Class="YourBackgroundClass" }) 

Then you can have a CSS rule such as:

.YourBackgroundClass { background:#cccccc; } 

If you want to apply a style directly you can do:

@Html.TextBoxFor(p => p.Publishers[0].pub_name, new { Style="background:#cccccc;" }) 
like image 127
medkg15 Avatar answered Sep 22 '22 05:09

medkg15


In my case I did like below. I have ASP.NET MVC application and we are using Bootstrap. I gave float:left to all my div elements. Just wanted to show how you can use @style along with @class for @Html.TextBoxFor

<div class="modal-body" style="height:auto; overflow-y:auto;max-height:500px;">   <table style="width:100%" cellpadding="10">      <tr>        <td>          <div style="display: inline; ">             <label style=" float:left; margin-right:20px;">Login Name: </label>             @Html.TextBoxFor(m => Model.UserPrincipalName, new { @id = "LoginName", @class = "form-control", @style = "float:left;margin-right:10px;margin-top:-5px;" })             <a href="#" onclick="SearchActiveDirectoryByLoginName()" title="Search Active Directory" class="btn btn-primary" style="float: left; margin-top: -5px;">                   @Html.Raw(" Search ")              </a>          </div>        </td>       </tr>   </table> </div> 

enter image description here

like image 40
Ziggler Avatar answered Sep 20 '22 05:09

Ziggler