MVC3 Razorview Syntax - asp.net-mvc

Can someone please tell me how to convert this to razor view syntax?
<%= ViewData.Model.Firms.Count > 0 ? Html.Pager(ViewData.Model.Firms.PageSize, ViewData.Model.Firms.PageNumber, ViewData.Model.Firms.TotalItemCount) : string.Empty%>
thanks for help in advance.

#if (Model.Firms.Count > 0)
{
#Html.Pager(
Model.Firms.PageSize,
Model.Firms.PageNumber,
Model.Firms.TotalItemCount
)
}
and if you want one liners:
#MvcHtmlString.Create(
Model.Firms.Count > 0
? Html.Pager(
Model.Firms.PageSize,
Model.Firms.PageNumber,
Model.Firms.TotalItemCount
).ToHtmlString()
: string.Empty
)
but personally I find the first far more readable.

You may want to simply make a helper instead that will take care of that logic, so your view can simply call #PagerTable(Model.Firms) which will figure out to write an empty string or call Html.Pager for you.

Related

Append strings if not nil

In the view of a rail's app, I want to achieve:
If user's height isn't nil: return the height value with string "cm"
Otherwise: return string "N/A".
I'm wondering if there's a way to do this. The code below:
user.try(:profile_name).try(:push("as Alias")) || "N/A"
isn't working. The part try(:push("cm")) gives me an error. I thought of using the << operator to append strings by using , but I think there should be a neater way to complete this. Is there anyone who can give me a hint?
--another similar example which I want to accomplish but not working:
user.try(:height).try(:to_s).try(:push("cm")) || "N/A"
How about:
(user && user.profile.present?) ? "#{user.profile} as Alias" : 'N/A'

Grails GSP null safety check trouble

I am trying to populate a text field on my GSP as such:
<label>Phone(aaa-bbb-cccc):</label>&nbsp<g:textField name="phone" style ="border-radius: 5px"
value="${recordToEdit.telephones = [] ? null : recordToEdit.telephones.first()}"></g:textField><br>
but it still tell me I can't access first() on an empty list. telephones is a List of Strings each being a phone number.
as #gross-jonas pointed out, the recordToEdit.telephones = [] ? .. : .. is terribly wrong already, unless it's a typo
the check you are trying to make should look like:
value="${recordToEdit.telephones ? recordToEdit.telephones.first() : ''}"
or
value="${recordToEdit.telephones?.getAt( 0 ) ?: ''}"
You can just use the Null Safe Operator (?.) as
${recordToEdit.telephones?.first()}
for null checks, which is not sufficient.
UPDATE
for empty list checks and null checks,
${ recordToEdit.telephones ? recordToEdit.telephones[0] : '' }
will be good.
Dude, didn't you just mean == instead of = ?
It looks like you are overwriting your telephones which get issued successfully instead of comparing it.

Ruby Ternary Operator does not seem to be working on a hash assignment

In the process of learning ruby (I have a java background).
I have assignment statements where the value of one hash[:name_field] is being assigned to another. But the value coming from the hash on the right was sometimes blank. This was crashing my code hence i added the ternary logic with .nil ? etc....
I am surprised though that this doesn't work... The error is :
undefined method `nil' for 1133:Fixnum (NoMethodError)
Below is the code:
people_traffic.each do |person|
person_record = DaysTraffic.new
person_record[:name] = person[:name_filed].nil ? 0 : person[:name_filed]
person_record[:age] = person[:age_field].nil ? 0 : person[:age_field]
person_record.save
end
Why am I getting the (NoMethodError) for the nil?
Thank you!
It should be .nil? (with a question mark) not .nil. So in your case, that would be:
person_record[:name] = person[:name_filed].nil? ? 0 : person[:name_filed]
You can actually write this much simpler like so:
person_record[:name] = person[:name_filed] || 0
Because #to_i turns nil into 0, a good way to write something like this is:
person_record[:age] = person[:age_field].to_i

Request parameter if struts2 if tag

I have another probably basic problem. happy if u can help.
there is a request parameter 'action'. if I write :
<label><s:property value="%{#parameters.action}"/></label>
the value appears (it is 1)
So itry to test now :
<s:if test="%{#parameters.action == '1'}">YES 1</s:if><s:else>NOT 1</s:else>
NOT 1 appears.
I have tries all the syntaxes I found on the net for the test. Nothing changes, NOT 1 still displays
Thank you
This is because:
the value of %{#parameters.action} is an array, not a single value, and
the value will be type-converted to a number (not sure why; need to look in to that)
The correct expression would be:
<s:if test="%{#parameters.action[0] == 1}">YES 1</s:if><s:else>NOT 1</s:else>
The correct expression would be:
<s:if test="#parameters.action[0] == 1">YES 1</s:if><s:else>NOT 1</s:else>
The request parameters is a map of [Strinf, String[]], So you have to access it like above

Short hand if statement

Is there a shorter version of the following:
Using ASP.NET MVC, this is in the HTML page
<%= IsTrue ? Html.Image("~/images/myimage.gif") : "" %>
I know I'm only really writing 3 extra characters, just wondering if there is something better.
It might be acceptable to create html helper:
public static string ImageIf(this HtmlHelper helper, condition, url){
return condition ? helper.Image(url) : "";
}
usage:
<%= Html.ImageIf(IsTrue, "~/images/myimage.gif") %>
No there is not, the ? operator is itself a short hand for the if else statement.
Not for the case you outlined.
If you are doing a null check on A you could write var b = A ?? string.Empty;
Kindness,
Dan

Resources