Currently it's returning the HTML as a string. Is there an easy way to get this to properly output HTML?
#Html.LabelFor(m => m.DisplayName,
Html.Raw(Html.Partial("_Tooltip", new Tooltip {
Title = Model.DisplayName,
Description = "This is my test description"}
).ToString())
.ToString())
How about using regular HTML?
<label for="DisplayName">
#Html.Partial("_Tooltip", new Tooltip { Title = Model.DisplayName, Description = "This is my test description" }
</label>
The problem you're running into is that the LabelFor helper automatically HTML-escapes the label, assuming that you're passing it just a regular string that shouldn't have HTML in it.
I was able to solve this by creating an extension method for it, following this example: http://weblogs.asp.net/imranbaloch/archive/2010/07/03/asp-net-mvc-labelfor-helper-with-htmlattributes.aspx
With the exception that I updated this:
tagBuilder.SetInnerText(innerText);
To this:
tagBuilder.InnerHtml = innerText;
If you do not want the raw html, remove the Html.Raw() function as follows:
#Html.LabelFor(m => m.DisplayName, Html.Partial("_Tooltip", new Tooltip { Title = Model.DisplayName, Description = "This is my test description" }).ToString().ToString())
Related
Hi I need to do the following:
#Html.TextBoxFor(m => m.Login,
new {
#class = "form-control",
#placeholder = "Username",
required="true" data-required-message="Please insert your name"
})
But Im getting error on data-required-message seems that I can't use "-".
Any clue?
You have to use it with underscore _ and razor will render it as - in html:
data_required_message="Please insert your name"
another thing to note is that you don't need to put # sign in front of every parameter of htmlAttributes , we put it for class becasue class is a reserved word in c#, so it cannot be used directly as a variable name.
your final code will be like:
#Html.TextBoxFor(m => m.Login,
htmlAttributes: new {
#class = "form-control",
placeholder = "Username",
required="true",
data_required_message="Please insert your name"
})
you are also missing a comma after placeholder attribute.
Try data_required_message instead of data-required-message
I have following HTML code. I want to get the href & title of the product & store them into different variables. I have tried following code.
within("div.product-action") do
#product_url = find("a.href")
end
But that throws an error.
Capybara::ElementNotFound: Unable to find css "a.href"
My HTML code is as follow:
<div class="product-action zoom" ng-class="{ 'logged-out': !user.$isLoggedIn }">
<a href="/g/women/christian-dior/so-real-sunglasses-colorless" title="Christian Dior So Real" Sunglasses-Colorless" ng-click="ProductUtils.cache(result)" class="bottom-action-container quickview-button hover-option" track="{
type: 'product-info',
name: moduleType,
breadcrumbs: result.breadcrumbs || breadcrumbs
}">
<i class="icon-zoom"></i>
</a>
</div>
a.href will select the a elements that have a href class. This is not what you want.
You can access the attributes as a hash after you found the element:
a = find('.product-action a')
href = a[:href]
title = a[:title]
You can find the href and title of given html code with below mentioned code:
within("div.product-action") do
productUrl = find(:css, '.bottom-action-container')[:href]
productTitle = find(:css, '.bottom-action-container')[:title]
end
OR
within("div.product-action") do
productUrl = find('a')[:href]
productTitle = find('a')[:title]
end
Hope this helps :)
Is there a way to pass a string with HTML tags without changing the Razor code?
Scenario (current code):
string msg = "<a href='http://www.google.com/html/'>Google</a>";
OUTPUT:
<a href='http://www.google.com/html/'>Google</a> on the page.
GOAL result:
Link to Google without changing the code "#msg".
try #Html.Raw(HttpUtility.HtmlDecode(msg));
You can try with
HtmlString msg = new HtmlString("<a href='http://www.google.com/html/'>Google</a>");
instead of
string msg = "<a href='http://www.google.com/html/'>Google</a>";
Hey You can edit your razor code as:
#{
HtmlString msg = new HtmlString("Hello <br> Hello Again");
<p style="text-align:justify;"> #msg </p>
}
It's simple
Benefit Set: </label><br />
<%: Html.DropDownListFor(model => model.bvODSMapping.Benefit_Set, Model.BenefitSet,new {id="BSet", style = "width:230px;" })%>
When I am using model=>model.bvODSMapping.Benefit_Set.Trim()
I am getting Value Can not be null.
can anybody help me out how to trim the string?
Thanks
I guess it would be the best to trim the values before passing the model to your view.
Nevertheless, this might help:
#Html.DropDownListFor(
model =>
model.bvODSMapping.Benefit_Set,
Model.BenefitSet.Select(
item =>
new SelectListItem
{
Selected = item.Selected,
Text = item.Text.Trim(),
Value = item.Value
}),
new { id = "BSet", style = "width:230px;" })
Currently when I want to set html attributes like maxlength and autocomplete, I have to use the following syntax:
<%= Html.TextBox("username", ViewData["username"], new { maxlength = 20, autocomplete = "off" }) %>
Is there any way to do this without having to explicitly set the ViewData["username"] portion? In other words, I want to rely on the helper method's automatic loading routine rather than having to explicitly tell it which field to load up from the ViewData.
Just pass "null" as second parameter:
<%= Html.TextBox("username", null, new { maxlength = 20, autocomplete = "off" }) %>
yes but you have to use ViewData.Model instead of ViewData.Item()
the code in your controller should look like this (sry 4 VB.NET code)
Function Index()
ViewData("Title") = "Home Page"
ViewData("Message") = "Welcome to ASP.NET MVC!"
Dim user As New User
Return View(user)
End Function
now you can do this in the view
<%=Html.TextBox("username", Nothing, New With {.maxlength = 30})%>
note that the user object has a public property username
hth
I used construction as below:
<%= Html.TextBox("username", "", new { #maxlength = "20", #autocomplete = "off" }) %>
For Setting max length of TextBox you can pass "" or null for Second Parameter and set html attributes(maxlength) as third parameter
<%=Html.TextBox("username", "", new { #maxlength = 10 }) %>