ASP.NET MVC: Optionally render element attribute - asp.net-mvc

I know there is some syntax with ? to optionally render attribute.
But I can't find it now that I need it ...
Something like:
< input checked=<%? model.IsActivated % > ...
Thanks.
EDIT: Since it seems that nobody knows... perhaps it was some different view engine.
Thanks for answers any way :)

<input checked="<%= model.IsActivated ? "checked" : string.empty % >"...
But the presense of the "checked" attribute might cause it to be checked, I can't remember. But if that's the case then you'll want
<input <%= model.IsActivated ? "checked=\"checked\"" : string.Empty %> ...
EDIT: btw its called the ternary(? or conditional) operator

Related

Is it possible to put multiple conditions in a th:classappend attribute

I've been racking my brains on this for a while and I'm pretty sure I'm getting the syntax wrong.
Basically I have a <div> and want it to have the class="hidden" unless two thymeleaf conditions are met.
I want the class 'hidden' unless there's an error present on the input.
This works on it's own:
th:classappend="${#fields.hasErrors('month') == false} ? 'hidden'
I want the class hidden unless the value of the input is not null.
This works on it's own:
th:classappend="${applicationData.month == null} ? 'hidden'"
Is there anyway to concat these two conditions into one th:classappend? I'm struggling with getting that working at the moment. Something like this doesn't seem to work for me
<div id="date-met" class="date-container" th:classappend="${#fields.hasErrors('month') == false} ? 'hidden' || ${applicationData.month == null} ? 'hidden'" >
<input id="month" name="month" type="text" th:value="${applicationData.month}">
</div>
Thanks in advance and apologies if it's a stupid question!
You can use:
( condition_one || condition_two ) ? 'hidden'
So in your case that would be:
th:classappend="${ ( #fields.hasErrors('month') == false || applicationData.month == null ) ? 'hidden' }">
The parentheses may not even be necessary - just added for clarity.

Rails - input default value from record

Perhaps I'm being silly but I've been trying to set a default value for an input with ruby on rails for hours and haven't cracked it.
I'm making a partial which can either allow users to create new records but will also show existing records if they exist. Code as follows
<input type="text" <%= (#prices.empty? || #prices.first.name.length == 0) ? 'placeholder="General admission"' : "value=" + #prices.first.name.to_s %> >
Which works perfectly for any value that exists UNLESS there is a space, for example, if price.name = "general admission" OR if price.name = "" (in which case it prints the placeholder) I get the following produced
<input type="text" admission'="" value="'general" id="event_price_name" name="[event][price][0][name]" aria-label="..." class="form-control">
It seems to get tripped up by the space.
Am I trying to use Rails in a way it wasn't designed to be done in? I'm more used to PHP which may be it!
Thanks
You should use the text_field_tag helper to build the input, this is preferred over building it with partial interpolation. Also, placeholder will automatically be overwritten if there is a value so you don't need to handle that part in the code, that's how it behaves by default on the browser.
<%= text_field_tag :admission, #prices.first.name.to_s, {placeholder: 'General Admission'} %>

Get the value of <option> from views to the controller

Since 'HTML' do not have the attribute 'name',I am looking for a way to accept the value, which I populated in views, in the controller and assign it a variable name for another use.
This is sample of my code:`
enter code here<form action="<%=url_for(:action =>:make_comment) %>">
<fieldset>
<select>
<%#category.each {|x|%>
<option id="label"> <%=x%></option>
<%}%>
</select>
</fieldset>
<input type="submit" value="submit"/>
</form>`
Are you saying that your html doesn't have a name attribute? If so, why?
The name attribute is what the server will interpret as the "key" for the value that you are sending. I don't know what other way you could try to go about doing this.
Maybe if you paste some code from your controller and your view, I could give a better answer.
Edit: After seeing your edited question, what you want to do is have your select tag with the name attribute, and your option tags have a value attribute with the value you want to send.
Hope this helps.
This example show how you create a select tag:
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {})
In this case, you would be able to get (in your controller) the value of the selected option through
params[:post][:person_id]
Is this what you want? Let me know if I misunderstood your question.

Why do Strongly Typed Html Helpers help me?

I read ScottGu's explanation on Strongly Typed Html Helpers and I understand that it gives me the ability to do better compile time checking of views. I was under the impression that I already had this when I used the model.PropertyName in the MVC1 Html.TextBox helper, but apparently that is not true. So, how does using a lambda expression do this better for me?
Consider the syntax of the existing HTML helper methods:
<%= Html.TextBox("Quantity", Model.Quantity) %>
If you rename the Quantity property on your object to "CurrentQuantity", the generated <input> element will still have name="Quantity" specified, and model binding will break if you don't remember to change that first parameter.
By using a lambda expression to specify the name of the element, an incorrect or misspelled property name becomes a compilation error.
<!-- No magic strings here! -->
<%= Html.TextBoxFor(model => model.CurrentQuantity) %>
The improvement comes when you specify the name of the property to the helper. With strongly typed helpers, it uses the lambda expression instead of the property name to determine which property value to use.
<%= Html.TextBox( "Name" ) %>
vs
<%= Html.TextBox( m => m.Name ) %>
Textbox does not give compile time error when you wrongly mentioned the property name. it will throw run time exception.
TextBoxFor is a genetic method so it will give compile time error when you wrongly mentioned the property name.
TextBoxFor will be helpful when we append two property names in view
#Html.TextBox( "Name" ,"value",new { #class="class"})
vs
#Html.TextBoxFor( m => m.Name, new { #id="txtValue"})

ASP .Net MVC, problem with checkboxes!

Basically I have a set of checkboxes that are dynamically created from view data like so:
<input type="checkbox" name="Calendars" value="<%= c.ID %>" /><%= c.Name %>
The value being the Calendar Id.
I can get what checkbox has been brought back using the FormsCollection its messy but it works!
(There also seems to be a bug with the checkbox helper that renders a hidden field next to the checkbox which means true is actually returned as "true,false"! I can work around this so its not an issue just thought Id mention it)
The problem comes when trying to hook the checkboxes up on an edit page!
I have a schedule class which can have multiple calendars and I want to show which calendars a schedule has by checking them on the edit!
My view is strongly typed but MVC magic can't map this!
Any ideas on whats the best way to do this??
I had tried passing the calendar ids in ViewData and do some inline code to check the appropriate checkbox but this is getting messy!
Thanks!!
UPDATE:
Done this
s.ID == c.ID).Select(s => s).Count() > 0) ? "checked=checked" : "" %>
You need to add "checked" tag manually to every check box:
<input type="checkbox" name="Calendars" value="<%= c.ID %>" checked="checked" /><%= c.Name %>
You dont need <input type="checkbox" - use Html.Checkbox(). It renders a hidden field next to the checkbox - but it is not a bug. From ASP.NET MVC source, InputExtensions.cs, line 201:
// Render an additional <input type="hidden".../> for checkboxes. This
// addresses scenarios where unchecked checkboxes are not sent in the request.
// Sending a hidden input makes it possible to know that the checkbox was present
// on the page when the request was submitted.
Use this:
<%= Html.CheckBox("Calendars", c.ID) %>

Resources