Why is #Html.Label() removing some characters - asp.net-mvc-3

When I use the following code in my razor view it renders <label for=""> someText</label> and not <label for="">1. someText</label> but I can't figure out why 1. is removed while rendering.
#Html.Label(String.Format("{0}. someText",1))
Edit:
The following code renders <label for="">1# someText</label> as expected.
#Html.Label(String.Format("{0}# someText",1))

You are misusing the Html.Label method. It is for:
Returns an HTML label element and the property name of the property
that is represented by the specified expression.
That's why it gets confused if you have a point . in the first parameter because it expects a property expression there.
However, you can use the second overload:
#Html.Label("", String.Format("{0}. someText",1))
Or just write out the HTML:
<label>#String.Format("{0}. someText", 1)</label>

You can avoid using the "Html Helper's label" and directly use html "label" and place whatever you want to display correctly. It can also save some time ;)

The syntax which you are using is wrong or We can say that this is not a way to use property with RAZOR syntax.
You ca use this that may be help full for you.
**
#Html.LabelFor(model => model.PropertyName,
String.Format("{0}. " + #Model.PropertyName.ToString() + ",1))
**

I was using this for a data table that contained a double (Lat/Long) and saw this same problem. Thanks for the tips (I am not allowed to comment).
For me, the problem was solved ..
#foreach (var cell in item.ItemArray)
{
<td>
#Html.Label("",cell.ToString().Trim())
</td>
}

Related

how to get the data using XPATH from div with display:none?

I want to extract data from a div element with the attribute 'display:none'.
<div class='test' style='display:none;'>
<div id='test2'>data</div>
</div>
Here is what I tried:
//div[#class = "test"]//div[contains(#style, \'display:none\')';
Please help.
Try several changes:
1) Just put normal quotes around "display:none", like you did for your class attribute and close with ]
2) Then your div with class test and your style attribute is one and the same, so you need to call contains also for the same div:
'//div[#class = "test" and contains(#style, "display:none")]'
or the quotes the other way around, important is, that you are using differnt quotes around the expression than inside the expression
"//div[#class = 'test' and contains(#style, 'display:none')]"
if this still does not work, pls post an error message

scrapy xpath : selector with many <tr> <td>

Hello I want to ask a question
I scrape a website with xpath ,and the result is like this:
[u'<tr>\r\n
<td>address1</td>\r\n
<td>phone1</td>\r\n
<td>map1</td>\r\n
</tr>',
u'<tr>\r\n
<td>address1</td>\r\n
<td>telephone1</td>\r\n
<td>map1</td>\r\n
</tr>'...
u'<tr>\r\n
<td>address100</td>\r\n
<td>telephone100</td>\r\n
<td>map100</td>\r\n
</tr>']
now I need to use xpath to analyze this results again.
I want to save the first to address,the second to telephone,and the last one to map
But I can't get it.
Please guide me.Thank you!
Here is code,it's wrong. it will catch another thing.
store = sel.xpath("")
for s in store:
address = s.xpath("//tr/td[1]/text()").extract()
tel = s.xpath("//tr/td[2]/text()").extract()
map = s.xpath("//tr/td[3]/text()").extract()
As you can see in scrappy documentation to work with relative XPaths you have to use .// notation to extract the elements relative to the previous XPath, if not you're getting again all elements from the whole document. You can see this sample in the scrappy documentation that I referenced above:
For example, suppose you want to extract all <p> elements inside <div> elements. First, you would get all <div> elements:
divs = response.xpath('//div')
At first, you may be tempted to use the following approach, which is wrong, as it actually extracts all <p> elements from the document, not only those inside <div> elements:
for p in divs.xpath('//p'): # this is wrong - gets all <p> from the whole document
This is the proper way to do it (note the dot prefixing the .//p XPath):
for p in divs.xpath('.//p'): # extracts all <p> inside
So I think in your case you code must be something like:
for s in store:
address = s.xpath(".//tr/td[1]/text()").extract()
tel = s.xpath(".//tr/td[2]/text()").extract()
map = s.xpath(".//tr/td[3]/text()").extract()
Hope this helps,

angular filtering decode characters

in angular, if
$scope.myStr = '™';
{{myStr}} yields '$trade;' instead of the TM mark, how would I solve this issue using a filter?
and in some cases, $amp;trade; also appears, so I would absolutely need a filter to run the procedures, and eventually I want to be able to {{}} the result without dom manipulation.
You can use ngBindUnsafeHtml: http://jsfiddle.net/Xnp3J/
<div ng-app ng-controller="x">
<span ng-bind-html-unsafe="myStr"></span>
</div>
-
function x($scope) {
$scope.myStr = '™';
}

How to check of a Node is inside a form tag?

Using XPath, how do I determine if a node is within a form tag? I guess I am trying to locate the form tag of its ancestor/preceding (but I couldn't get it to work).
example 1:
<form id="doNotKnowIDofForm">
<div id="level1">
<span id="mySpan">someText</span>
</div>
</form>
example 2:
<form id="doNotKnowIDofForm">
This is a closed form.
</form>
<div id="level1">
<span id="mySpan">someText</span>
</div>
</form>
I can use xpath "//span[id='mySpan']" to locate the span node. But I would like to know if mySpan is inside a form (I do not know the id of the form). I have tried "//span[id='mySpan']/preceding::form/" and "//span[id='mySpan']/ancestor::form/"
Thanks in advance.
EDIT: I would like the XPath to select the myForm form tag in Example1 but NOT in Example2
I'm not 100% sure from your description whether you're looking to select the form element, or the span element. It seems more likely that you're going for the form, so I'll address that first.
Your XPath with the ancestor::form would have been ok if it didn't have the slash at the end, but it's more roundabout than it needs to be. I think this is a better way:
//form[.//span/#id = 'mySpan']
or this:
//form[descendant::span/#id = 'mySpan']
To produce an XPath that locates certain nodes only if they are within a form, you would put the ancestor::form inside the predicate:
//span[#id = 'mySpan' and ancestor::form]
or you can do this, which would again be more straightforward:
//form//span[#id = 'mySpan']
Your own attempt
//span[id='mySpan']/ancestor::form/
looks fine to me.
You can simply use,
"form//span[id='mySpan']"

How to read id value of a DIV element using Selenium WebDriver?

<div id="ctl00_ContentHolder_vs_ValidationSummary" class="errorblock">
<p><strong>The following errors were found:</strong></p>
<ul><input type="hidden" Name="SummaryErrorCmsIds" Value="E024|E012|E014" />
<li>Please select a title.</li>
<li>Please key in your first name.</li>
<li>Please key in your last name.</li>
</ul>
</div>
here is my snippet for example. i want to get the value of ID i.e., ct100_contentHolder_vs_ValidationSummary. using selenium web driver. h
You can try this :
String id=driver.findElementByXpath("//div[#class='errorblock']").getAttribute("id"));
But in this case the class of this division should be unique.
Use following code to extract id of first div:
WebElement div = driver.findElement(By.tagName("div"));
div.getAttribute("id");
This is the code for all div available on the page:
List<WebElement> div = driver.findElements(By.tagName("div"));
for ( WebElement e : div ) {
div.getAttribute("id");
}
I know this answer is really late but I wanted to put this here for those who come later. Searching by XPath should be avoided unless absolutely necessary because it is more complicated, more error prone, and slower. In this case you can easily do what the accepted answer did without having to use XPaths:
String id = driver.findElement(By.cssSelector("div.errorblock")).getAttribute("id");
Some explanation... this line finds the first element (.findElement vs .findElements) using a CSS Selector. The CSS Selector, div.errorblock, locates all div elements with the class (symbolized by the period .) errorblock. Once it is located, we get the ID using .getAttribute().
CSS Selectors are a great tool that all automators should have in their toolbox. There's a great CSS Selector reference here: http://www.w3.org/TR/selectors/#selectors.

Resources