MVC3 title property on #Html.TextBox - c#

I have a C#.Net MVC3 web app. I am adding properties to textboxes (and drop down lists) using the HTML helper functions.
#Html.TextBox("Date", String.Format("{0:MM/dd/yyyy}", Model.Date),
new { #class = "datepicker", #title="Date mouse over text" })
Is there a way to format the #title text?
CLARIFICATION: Italics, bolding, coloring....that type of formatting.

this has a general tip on creating a styled tooltip
How to change the style of Title attribute inside the anchor tag?
you'll probably need to create your own htmlhelper that creates the textbox with the tooltip span described in the referenced question

#Html.TextBox("Date", String.Format("{0:MM/dd/yyyy}", Model.Date),
new { #class = "datepicker",
title = string.Format("{0}, {1}", "Hello", "World") })
The only reason you use the # symbol before class is because class is a C# keyword. You would also have to use it for attributes named default, namespace, double, etc.
Title is not a C# keyword. Therefore you do not need to prefix it with the # symbol.

I personally like this one:
jquery.tipTip
You only have to add jquery and tipTip to your page. Then add a short script:
$(function(){
$(".someClass").tipTip();
});
Then your textbox should be:
#Html.TextBox("Date", String.Format("{0:MM/dd/yyyy}", Model.Date),
new { #class = "datepicker someClass", #title="Date mouse over text" })

Related

#Html.Label is not displaying when content having dot (.) - Bootstrap MVC Razor

I am populating label text dynamically from database
<div class="col-11 col-sm-11 col-md-11 col-lg-11 col-xl-11">
#Html.Label(#Model.Questions[i].DataText + ": ", new { #for = "dd" + #Model.Questions[i].DataTextId, #class = (#Model.QDataText[i].IsMandatory ? "required-field" : ""),Style= "white-space:normal; text-align: right;" })
</div>
But Label is not displaying while Label content contains a dot (.).Can anyone please help to fix the issue.Content that I am trying to display inside the label is as given below. For me it is showing only "In this" on the view.
"defines disrespect as an expression of lack of respect and a fashion that is generally disrespectful and contemptuous. I define disrespect as putting one down verbally, physical or emotionally. In this essay I will be talking about the research I did on article 88 and 91 of the uniformed code of military justice. Disrespect and insubordination in the army affects the ability of a unit to maintain discipline and order. The uniformed code ofshow more contentIt could also cause the soldier notdddefines disrespect as an expression of lack of respect and a fashion that is generally disrespectful and contemptuous. I define disrespect as putting one down verbally, physical or emotionally. In this"
I'd just stick with something like this:
<label>#(Model.Questions[i].DataText): </label>
you can use
#Html.LabelFor(model => model.FieldName, "Edited By", htmlAttributes: new { #class = "control-label", style = "width: 90%;" })

Duplicate name attribute generated for the HTML Helper

I get a duplicate name attribute while validating through https://validator.w3.org
#Html.EditorFor(model => model.User_Name, new { htmlAttributes = new { #class = "form-control", id = "txtFullNameRealtor", #Name = "txtFullNameRealtor", placeholder = "Full Name" } })
I get this in the html source
<input Name="txtFullNameRealtor" class="form-control text-box single-line" id="txtFullNameRealtor" name="User_Name" placeholder="Full Name" type="text" value="" />
As you can see there,two name attributes are generated that is Name="txtFullNameRealtor" and name="User_Name".
Is there a way to make it generate a single name attribute ?
And I want the Name attribute be explicitly set by me to be there.
You could acheive this with the following:
#Html.TextBox("txtFullNameRealtor", Model.User_Name, new { #class = "a-class-name" })
As it looks like you want a text box anyway you can use the text box helper. The first argument is where the name/id are derived from, the second argument is the value and the 3rd are the htmlAttribtues. There are more overloads as well.
User Html.Editor() instead of Html.EditorFor().
EditorFor will infer the html element name from the model property that is "bound" to it.
Editor allows you to declare whatever html name you would like with the last parameter.
public static MvcHtmlString Editor(
this HtmlHelper html,
string expression,
string templateName,
string htmlFieldName
It sounds like you are binding a domain model directly to your UI. I'd suggest create a viewmodel with the property names you want and translating between your view model and domain model in your controller instead.
You can try this
#Html.EditorFor creates name same as that of property name.
if you want to specific name you can use #Html.Editor instead of #html.EditorFor. i hope this helps
#Html.Editor("txtFullNameRealtor" er_Name, new { htmlAttributes = new { #class = "form-control", id = "txtFullNameRealtor", placeholder = "Full Name" } }

How do you add a placeholder using #Html.NopEditorFor

I am trying to add a placeholder to my text field.
I am fairly new with MVC and only been working on it for the pass month.
My code looks like this:
Branch Hours Description: #Html.NopEditorFor(model => model.Description)
BRHOURID: #Html.NopEditorFor(model => model.BrHourCode)
is it possible to add a place holder for these 2 fields?
You need to add a HTML attribute
Branch Hours Description: #Html.NopEditorFor(model => model.Description, new { #placeholder = "Add Your Placeholder Text Here" } })
and that should do it for you

Dynamically Modify value of #Html.HtmlHelper

I am trying to modify a behavior for DataAttributes (Maxlength[int] or StringLength[int]), where it would add a "maxlength" attribute inside (textarea) tag along with its "int" value. I have already performed some research, and the closest answers I obtained were this post and that question.
However, I hope to go one step further.
I already know that StringLength adds "data-val-length-max" and "data-val-length" attributes to the tag.
I spent a lot of time trying to implement solution in JavaScript unobtrusive validation file by using logic
if tag has data-val-length-max attribute
add maxlength attribute to this tag
assign its value equal to data-val-length-max value
However, in my project, Javascript files are constantly loaded/unloaded and it is extremely time-consuming to keep track of the logic flow, so I decided to change the approach, and try to implement it within C#.
Inside my .cshtml file, there are lines like following:
#Html.myLabelFor(model => model.Project.ProjectDescription, new { #title = "Enter a description for this project" })
#Html.TextAreaFor(model => model.Project.ProjectDescription, new { #class = "textfield_Paragraph", #id = "ProjectDescription" })
#Html.ValidationMessageFor(model => model.Project.ProjectDescription)
I learned that it successfully adds maxvalue if it adds #maxlength at the end:
#Html.TextAreaFor(model => model.Project.ProjectDescription, new { #class = "textfield_Paragraph", #id = "ProjectDescription", #maxlength = 50 })
However, I do not want to manually add #maxlenght this line to every TextAreaFor line in solution; I want to turn it into reusable code, that would dynamically perform such action to every variable within TextAreaFor inside the .cs file, which are marked like following:
[MaxLength(50)]
[Display(Name = "Project Description")]
public string ProjectDescription { get; set; }
So I am thinking of implementing it by using the logic
if metadata has a MaxLength flag
htmlAttributes += " ', #maxlength = ' + maxLengthValue"
So, physically it will still be
#Html.TextAreaFor(model => model.Project.ProjectDescription, new { #class = "textfield_Paragraph", #id = "ProjectDescription" })
while the final value that will be loaded will be
#Html.TextAreaFor(model => model.Project.ProjectDescription, new { #class = "textfield_Paragraph", #id = "ProjectDescription", #maxlength = 50 })
I want to know if something like this is even possible to implement, so I know beforehand if I should continue thinking in that direction.
Again, links I have posted above were the closest answers I could get, so I am curious if it is as far as I can get, or there is even more room to improve. My huge preference would be NOT touching .js files unless I absolutely HAVE to.

Razor label dot period

I am using mvc5 in combination with razor and fill website text dynamically based on language. example:
#Html.Label(#DDHelper.GetContent("user_name"), htmlAttributes: new { #class = "control-label col-md-3" })
#DDHelper.GetContent("user_name") returns a string, which is set as a label text. when #DDHelper.GetContent("user_name") returns "Hello." the label is not created and the html source is empty.
I know this is because #HTML.Label() does not allow '.' and that's the root of my problem and that I should use #HTML.LabelFor() but how can i use #HTML.LabelFor() when i just want to display a string?
Check the overloads for #Html.Label, the one you're using is:
public static MvcHtmlString Label(this HtmlHelper html,
string expression,
object htmlAttributes)
the one you want is
public static MvcHtmlString Label(this HtmlHelper html,
string expression,
string labelText,
object htmlAttributes)
There's a common misunderstanding of what an HTML label is. On winforms, a label is where you put some text - this is not the case in HTML. In HTML a <label> is to allow the user to click on your text and have the focus/cursor point to the corresponding input.
The full syntax for a label is:
<label for="controlName">caption</label>
The 'expression' part in the overloads above is the "for" part in the html - it must point to a control name.
If this is what you are trying to do (pair a label with a control) then try:
#Html.Label("user_name",
#DDHelper.GetContent("user_name"),
htmlAttributes: new { #class = "control-label col-md-3" })
Where your input is named 'user_name'. This is where #Html.LabelFor comes in, something like:
#Html.Labelfor(model=>model.UserName,
#DDHelper.GetContent("user_name"),
htmlAttributes: new { #class = "control-label col-md-3" })
so you don't hard-code field names and they refactor.
If you "just want to display a string" then you probably don't want a <label>. If you just need some simple text, you don't need an #Html anything and can just output the translation:
<div class='control-label col-md-3'>
#DDHelper.GetContent("user_name")
</div>
but as you're using 'control-label' I suspect you do want <label>.
If Html.Label(..) is not an option I'd suggest an alternative:
<h4>#Html.Encode(#DDHelper.GetContent("user_name"))</h4>
Worked fine for me.

Categories