Assign ViewBag value to input checkbox? - asp.net-mvc

I am trying to assign ViewBag value in Boolean to an HTML Input checkbox. It is throwing the following error: Cannot resolve symbol '<%= ViewBag.Solicitation %>'
<input id="chkSolicitation" type="checkbox" name="chkSolicitation"
checked="<%= ViewBag.Solicitation %>" />

I recommend using this, assuming you are using Razor:
#Html.CheckBox("chkSolicitation", (bool)ViewBag.Solicitation)
If you are not using Razor, use this:
<%: Html.CheckBox("chkSolicitation", (bool)ViewData["Solicitation"]) %>

Related

Thymeleaf shows null on form when using th:field?

Using Spring with Thymeleaf (spring-boot-starter-thymeleaf) current version. When I have a form where some fields are null, Thymeleaf is showing null in the input control. How do I make it just show blank?
This is only happening when I use fetch and replace a div. If I open a window from the URL the content is fine. What's happening?
TIA
<label for="address1">Address:</label>
<input type="text" name="address1" id="address1" th:field="*{address1}"> <br>
<label for="address2"> </label>
<input type="text" name="address2" id="address2" th:field="*{address2}"> <br>
Nevermind. I realized I had some binding code that wasn't checking for that.

Interpolating ERB as an HTML value attribute (Rails)

I am attempting to pass data as params in a hidden input tag's value attribute:
<input name="quote[destination]" value= <%= "#{quote["InboundLeg"]["OriginCity"]}" %> type="hidden" />
When the form is submitted, the params contains the first word of the string being interpolated and drops everything after the first space.
If I decide to pass through an ordinary string to params through value like so:
<input name="quote[destination]" value= "foo bar buzz" type="hidden" />
the entirety of the string passes through unlike the former case. Can anyone shed some light on why this may be and some possible solutions?
Change that line to:
<input name="quote[destination]" value="<%= quote["InboundLeg"]["OriginCity"] %>" type="hidden" />
Note that the quotes are outside of the ERB statement.
Or you might want to use the hidden_field_tag form helper which creates such hidden input fields and reads nicer:
<%= hidden_field_tag 'quote[destination]', quote["InboundLeg"]["OriginCity"] %>
I would always use a helper if I have a choice because IMHO is a bad practice to mix HTML and ERB like in the first example.
Replace your input tag with
<input name="quote[destination]" value= "<%= quote['InboundLeg']['OriginCity'] %>" type="hidden" />
You have to apply quotes to the value

ASP.NET MVC using bool value for radio button checked property

Basically I want my radio buttons to be checked depending on a value from my model view control, below is some sample code and my attempt so far:
<tr>
<td>
<label for="brakepads" class="label3">Brake Pads:</label></td>
<%= Html.TextBox("brakepads", Model.brakepads, new {style="display:none" })%>
<%= Html.ValidationMessage("brakepads", "*") %>
<td><label for="brakefluid" class="label4" >Passed:</label>
<asp:RadioButton ID="RadioButton15" runat="server" GroupName="b" value="True" Checked="<%= Model.brakepads.Value %>" onclick="brakepads.value=(this.value)" /></td>
<td><label for="brakefluid" class="label4" >Failed:</label>
<asp:RadioButton ID="RadioButton16" runat="server" GroupName="b" value="False" Checked="<% Model.brakepads.Value %>" onclick="brakepads.value=(this.value)"/></td>
</tr>
I get the following at runtime: Cannot create an object of type 'System.Boolean' from its string representation '<%= Model.brakepads.Value %>' for the 'Checked' property.
I tried using (bool) to cast, but that didn't work either, so I'm unsure of what to do next. My only concern is that occasionally the bool will be null, if that won't break functionality then it's fine.
How to do a boolean radio button list in ASP.NET MVC 3 razor C#
What we want the html to be
Apply discount:
<input type="radio" name="apply_discount" value="yes" /> Yes
<input type="radio" name="apply_discount" value="no" /> No
We need a model property
public bool ApplyDiscount { get; set; }
Now all we have to do is convert the html to razor
Apply discount:
#Html.RadioButtonFor(m => m.ApplyDiscount, true) Yes
#Html.RadioButtonFor(m => m.ApplyDiscount, false) No
I hope this helps.
You are mixing WebForms controls (RadioButton) together with ASP.NET MVC helpers (Html.TextBox), I think it's a dangerous practice.
You could try using Html.RadioButton() helper method to render a radio button:
<td><label for="brakefluid" class="label4" >Passed:</label>
<%= Html.RadioButton("brakefluid", true, Model.brakepads.Value) %></td>
<td><label for="brakefluid" class="label4" >Failed:</label>
<%= Html.RadioButton("brakefluid", false, !Model.brakepads.Value) %></td>
Either you use Html.RadioButton or have another MVC hidden field and bind Model.brakepads to it. Use javascript to set radio button's Checked property.

ASP.NET MVC: set custom name to rendered html element

I generate html textbox in this way:
<%: Html.TextBoxFor(m => m.Category) %>
ASP.NET MVC render html:
<input type="text" value="" name="Category" id="Category">
Is there is a way to set manually name of the textbox not eqaul to property "Category", but something else?
Thanks.
<%: Html.TextBox("someOtherName") %>
You have to change the default editor template or add a new template for this. You can refer this blog post from Brad Wilson for some insight.

asp.net mvc Html.Textbox, unable to set value?

in my route table I have this entry
routes.MapRoute(
"myRoute",
"route/{controller}/{action}/{id}/{start}/{end}",
new { controller = "Home", action = "Index", id = "", start="", end="" }
);
in my master page I have a line of code like so:
<%= Html.TextBox("foo", "bar") %>
If I access the page in the form of http://mysite.com/route/Home/Index/id/start/end the textbox renders OK with a value of "bar"
However if I access the page using the default parameters http://mysite.com/route/ the textbox does not have a value! In the emitted HTML it shows up like so:
<input id="foo" type="text" value="" name="foo"/>
it didn't set the value to "bar"...is this a bug? or is this not allowed in mvc master pages?
it should work fine
" <%= Html.TextBox("name", "Please enter your name...")%>
Output : < input id="name" name="name" type="text" value="Please enter your name..." />
<%: Html.TextBox("foo", "bar") %>
sometimes you need to force it to be a simple html attribute as follows
<%: Html.TextBox("foo", null,new{value="bar"}) %>

Resources