Razor label dot period - c#

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.

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%;" })

How to add and empty select list with Html.DropDownList

I am trying to add a drop down select element to my .net page, but I need it to be black so I can populat it with a json list I have with jQuery. Right now, my view has this:
#Html.DropDownList("Language", new SelectList(" "), "name = 'Laguange'", new { #class = "Language-List field form-group form-control" })
This 'works', but it seems to be confusing some stuff when I try to submit the form it is in. The value I select from it is never seen by the model, as it keeps triggering my [Required] validation helper.
Does anyone know How to add this without the new SelectList(" ") attribute?
I would suggest it's because youre using the name attribute in the wrong place.
Try this:
#Html.DropDownList("Language", new SelectList(" "), new {Name="Language", #class = "Language-List field form-group form-control" })
Notice the capital Name
Although as they are the same, you can just leave it out:
#Html.DropDownList("Language", new SelectList(" "), new {#class = "Language-List field form-group form-control" })

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

MVC3 title property on #Html.TextBox

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" })

Categories