how to use text tag in MVC 3 razor - html

i want to use regex on the views i have in MVC 3 page. how i can use
when i wrap them with text tag they not work ex:
<text> var pattern = #fjkfdkl</text>
i not want to put ## instead of # on every pattern. well what is the way and rule for using text tag in MVC

When you wrap something in a text tag your are saying to Razor that "this is text" not code. If you want code you can then do a code block like:
<text>#{ var pattern = fjkfdkl; }</text>
If you are doing this in some sort of loop you can just continue writing your code:
foreach(var o in listOfObjects) {
var pattern = fjkfdkl;
}
In the above example razor knows whats code and what is not. You can then expand on the above example if you want to put markup in the loop:
foreach(var o in listOfObjects) {
var pattern = fjkfdkl;
<text>
Hello World!
</text>
}
or
foreach(var o in listOfObjects) {
var pattern = fjkfdkl;
<p>
Hello World.
<p>
}
You only really need to use the <text></text> tags inside of loops where you don't have any html tags.
Razor is smart enough so when you open your tag inside a loop e.g. <p> it know until that tag is closed then its in markup. When it is closed it will then look for a } for the closing of a loop (or another html tag).

Related

Using jquery to have one html element point to another

I am new to jquery and was wondering how I can point one html element equal to another. I want to make it so that whenever something in the h2 tag changes, the text within the p tags will copy the change. Below is how my tags are set up within the class fc-center.
var title = $('div.fc-center h2').text();
$('.fc-center').append('<p>'+'' +'</p>');
with the html looking something like
<div class = 'fc-center'>
<h2> text text</h2>
<p> </p>
</div>
essentially what I want to do is something like this :
$('div.fc-center p').equalto $('div.fc-center h2')
But I am not quite sure how to go about it
I propose this solution:
var title = $('.fc-center').find('h2').text();
var elementsP=$('.fc-center').find('p');
if (elementsP.length > 0) {
$.each(elementsP, function(i, val) {
$(this).empty().html(title);
});
}
https://jsfiddle.net/julian9319/grc0y6qf/1/

AngularJs - how to support a textbox with hyperlinks

I'm new to Angular but I'm trying to implement a textbox that allows users to enter in links. I only want to support links, and otherwise I want to block all html from being presented as such. I could theoretically use something other than a textarea, but my requirements are that it must be bound to a variable in my scope (right now with ng-model) and I cannot accept html tags other than '< a >'
Here is my example plnkr
In the example, I would like the second seeded item to display as a link, blue and underlined. However, the third item should display as it is currently shown (without interpreting it as html).
HTML:
<textarea maxlength="160" ng-model="val.text"></textarea>
<div class="btn" ng-click="submit()">Submit</div>
<br><br>
<div ng-repeat="item in items">
{{display(item)}}
</div>
JS:
$scope.submit = function() {
if (!$scope.val.text) return
$scope.items.push($scope.val.text);
}
$scope.display = function(txt) {
return txt;
// something here? if txt contains <a> and </a> indicate
// that we should display as html
}

Add non breaking space within razor helper method

How can I include a non-breaking space ( ) within a Razor helper method? Here's the helper in question:
#helper RenderClipResult(Clip clip, IList<string> searchTerms)
{
<div class="result">
<!-- other clip stuff -->
#if (clip.ThirdPartyMaterials != null && clip.ThirdPartyMaterials.Count > 0)
{
<p>
<span class="heading">Third Party Material</span><br/>
#foreach (var material in clip.ThirdPartyMaterials)
{
#AddElement("Description", material.Description, searchTerms) #AddElement("Cost", material.Cost, searchTerms)
<br />
}
</p>
}
</div>
}
AddElement is another custom helper. The output I'm looking for is something like this:
Third Party Material
first entry
second entry
third entry
I could wrap the AddElement line in a span tag for styling but it's another html tag and css rule, just to indent some text by a single character width. Might have to go that way as Razor is not able to parse the space
Add #: before your non-breaking space html code
The second option is to use construction: #Html.Raw(" ") . This also can be user with multiplied nbsp-signs, so you can put result of the string builder to RAW function.

Regular expression for selecting attributes name only from within certain tags

What's the regex which allows me to select all the attribute names from <form> and <input> tags but not from any other HTML tag?
For example:
<!-- all attribute names get selected -->
<input class="something" id="yes" type="text" name="my-field" value="Hello, world!">
<!-- class and id don't get selected because it's a div -->
<div class="something" id="no"></div>
<!-- class gets selected -->
<form class="my-form"></form>
I'm only after the attribute names
Such a regexp would be very complicated to build. Despite the fact that you can't match all HTML by regexes, it would need a very complicated lookbehind to check whether the attribute name which you want to match comes after a opening tag whose name is either "form" or "input". Don't try to build such a regex, you'd go crazy and/or end up with an unreadable, non-maintainable or -undestandable monster.
Instead, use a DOM parser (there will be one for your language) and apply DOM selectors and get the attribute names of the elements.
It is not easy task to do it with regex and actually it is not a good idea to do it with regex. But it is possible >>
input = '...';
var tmp = input, found, params = [];
var re = /(<(?:form|input)\b.*?\s)([\w\-]+)=(['"]).*?\3/gi;
do {
found = 0;
tmp = tmp.replace(re, function($0,$1,$2,$3) {
params.push($2);
found = 1;
return $1;
});
} while (found);
Check this demo.

Razor HTML Conditional Output

I have a list of items I want to output as the contents of a main (the main in not included below). Each Item has 3 attributes: a Section Name, a Label and a Value. Each item is enclosed in a and everytime the Section Name changes I have to open a (and close the previous one, if any). I'm using a Razor view with this code:
#foreach (LocalStorageItem lsi in Model) {
string fld_name = "f_" + lsi.ItemName;
if (lsi.SectionName != sn) {
if (sn != "") {
Html.Raw("</fieldset>");
}
sn = lsi.SectionName;
<h2>#sn</h2>
Html.Raw("<fieldset>");
}
<div class="row">
<div class="ls_label">#lsi.ItemName</div>
<div class="ls_content" name="#fld_name" id="#fld_name">.</div>
</div>
}
#if (Model.Count != 0) {
Html.Raw("</fieldset>");
}
The problem is: each time the Section Name changes no fieldset tag (open and/or close) is generated. Where am I wrong? If I don't use Html.Raw (or #: as an alternative) the VS2010 parser signals an error.
Calling Html.Raw returns an IHtmlString; it doesn't write anything to the page.
Instead, you should write
#:</fieldset>
Using #: forces Razor to treat it as plain text, so it doesn't need to be well-formed.
However, your code can be made much cleaner by calling GroupBy and making a nested foreach loop.
I really think that the use of #: to work around such code is an abuse of that escape sequence. The problem should be addressed instead by correctly refactoring the code so that balanced tags can be easily written:
#foreach(var section in Model.GroupBy(i => i.SectionName)) {
<h2>#section.Key</h2>
<fieldset>
#foreach(LocalStorageItem lsi in section) {
string fld_name = "f_" + lsi.ItemName;
<div class="row">
<div class="ls_label">#lsi.ItemName</div>
<div class="ls_content" name="#fld_name" id="#fld_name">.</div>
</div>
}
</fieldset>
}
12 lines of code instead of 18