Using a Ternary Conditional with in Asp.Net MVC Razor - asp.net-mvc-3

I am trying to get the following ternary conditional to work in Asp.Net MVC 3 Razor:
Next
All of the examples I am finding of using a ternary conditional in Razor have return values which are strings. But here I would like to use an expression (Model.PageNumber + 1) and return a number. Is this possible?

Drop the # sign before the value:
Next
Let me just add that in general, Razor doesn't need/want the # prefix unless it's absolutely necessary, for example:
<div>
#foreach(var value in Model.Values)
{
if(value.Flag)
{
<div>#value.Text</div>
}
}
<div>
Notice that you don't need a second # sign until you're actually inside the tag, where Razor wouldn't know whether you wanted to display the text "value.Text" or execute it as code. The if statement is assumed to be code. To escape this and write the line "if(value.Flag)" as text you'd need to explicitly say so with the #: prefix.

Related

Oracle Apex Force Upper Case first Letter.

I Guys
In forms I use,
onKeyUp="this.value = this.value.toUpperCase()"
To force upper-case. However for such as name fields. How do you force the upper letter to be upper-case only while the user is typing. I know INITCAP will do that but need to do as user is typing, if that makes sense.
Any help will be much appreciated.
This is a javascript question then, not and Oracle or APEX question. It shouldn't make any difference what the environment is as long as you have access to the DOM events with javascript functions. e.g. http://www.w3schools.com/jsref/event_onkeyup.asp
If you do a search there are lots of examples to Initcap a string in javascript, just pass in the string and reset the item in the dom e.g.
function capitalizeEachWord(str) {
return str.replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
I tried to solve this problem.
For that I created JavaScript function which check first letter capital ,if not then it display alert and revert text.
please check following code for text item:
function checkUpper()
{
var x = $x("P6_TEXT");
if (x.value.trim().substring(0,1) != x.value.trim().substring(0,1).toUpperCase()) {
alert ('First letter Must be in upper case');
x.value = x.value.trim().substring(0,x.value.trim().length-1).toString();
}
}
And set item P6_TEXT attribute as
onKeyUp="checkUpper();"
In the field custom attributes put this JS code:
onKeyUp="this.value = this.value.substring(0,1).toUpperCase()+this.value.substring(1).toLowerCase();"
You could use content modifiers from Universal Theme https://apex.oracle.com/pls/apex/apex_pm/r/ut/content-modifiers
I needed text in a page item to be uppercase and under Advanced I set the css classe to
u-textUpper
u-textInitCap - Sets The First Letter In Each Word To Use Uppercase

How to use AjaxResponse.Render() to replace a span's text in MVC?

I am replacing 2 divs using with AjaxResponse.Render() which have 2 associated partial views with them.
But there a span like the following:
<span id="total-counts">23 comments</span>
I need to replace this value as well. How do I call the with AjaxResponse.Render() to replace this value?
There is no associated view with this span. It might be easy thing but I can't make it work.
I tried these:
Note the second param is the view name.
AjaxResponse.Render("#total-counts", "string", comments.Count, UpdateStyle.Replace);
AjaxResponse.Render("#total-counts", "", comments.Count, UpdateStyle.Replace);
None of these work. I don't want to create a partial view unless it is the only way.
Thx
On Ajax Success Callback use as
$("#total-counts").html(' ');
$("#total-counts").html(comments.Count+" Comments");

How to prevent thymeleaf from replacing special characters in HTML attribute values?

I'am using Thymeleaf in combination with a js micro-templating routine, which result in special characters in attribute values. When running Thymeleaf on
<a style="display:<%= x ? 'block' : 'none' %>;">
it creates
<a style="display:<%= x ? 'block' : 'none' %>;">
while I would expect to get exactly the same I put into the processor.
How do I use special characters in HTML attribute values?
Many thanks!
Try to play with
th:utext="#{unescaped text}">
See Thymeleaf doc Unescaped Text
There is some variants to review:
use LEGACYHTML5 option in templateResolver as mentioned above
override template method in Underscore.js
I prefer second approach. Place this code in your initialization script:
var original = _.template;
_.template = function(content) {
// fix operators escaped by Thymeleaf HTML5 validator
content = content.replace(/'/g, "'");
content = content.replace(/</g, "<");
content = content.replace(/>/g, ">");
return original.call(this, content);
};
You can try to surround your code with a CDATA block.
http://www.w3schools.com/xml/xml_cdata.asp
I'm not sure about the other template modes but I know html5 and xml are not going to let you do this since the document thymeleaf would be generating would NOT validate against the doctype. So at least in those modes, I don't think this is possible. (Maybe with a custom dialect?)
So why the desire to use two templating tools on the same page anyway?
As hubbardr already suggested, using LEGACYHTML5 as template mode might help you here.

Express JS Jade Engine If statement nested in For loop

I can't seem to find a solution to this.
I'm trying to nest a if statement inside a for loop in Jade engine (using express js).
The base code is shown below:
form
select
for obj, i in phoneModel
option(value='#{i}') #{obj.phone_model}
What I would like to do is to have a IF statement inside the for loop to check to see if a varaible "deviceIndex" is a certain value. Eg. If deviceIndex == i, then do something, else do some other thing.
I have tried the code below:
form
select
for obj, i in phoneModel
- if(phoneIndex == #{i})
option(value='#{i}') #{obj.phone_model}
- else
option(value='#{i}' selected='selected') #{obj.phone_model}
It gives the "expect indent, but got newline" error. I expect it is my placement of the if statement inside the for loop; however, I have tried just about every combination of tabs and spaces as well as putting the "option(val..." line inside a bracket on the same line as the if statement.
What's with the typeof around a boolean? And shouldn't the phone with phoneIndex == i be the one selected? Also, the point of Jade is to have much cleaner code. Tell me if this works:
form
select
for obj, i in phoneModel
option(value=i, selected=phoneIndex==i)= obj.phone_model

No overload for method 'Write' takes 0 arguments

what's wrong with that code ? I'm getting the error from this topic's title.
#{ var errors = ViewData.ModelState.Values.Where(x=>x.Errors.Count > 0).ToList();}
SOLVED, look # the comments
Inside a using statement or other code block, Razor expects code, not markup.
Therefore, you must put in code directly, not in #{ ... } blocks.
The Razor parser interprets your code as # (printing an empty expression), followed by a normal C# statement block ({ ... }).
You only use #{ ... } blocks to put code where Razor is expecting markup.

Resources