All radioButtons are false at the view - asp.net-mvc

#Html.RadioButton("smth",true)
This row at the page looks like UNCHECKED radioButton. Why?

You should use
HtmlHelper.RadioButton(string name, object value, bool isChecked)
extension method.
First argument is the name of the form field which is input field generated by html helper.
Second argument is the value of input element. If this radio is selected when the postback to server happens, this value is used.
Third argument is what you are looking for. If it is true it makes radio button selected.
For instance,
#Html.RadioButton("Name", "Value" ,true)
would generate an input element which looks like following,
<input checked="checked" id="Name" name="Name" type="radio" value="Value" />

You need to use something of the form
#Html.RadioButton(id,value,checked (bool true/false))
So
#Html.RadioButton("A","B",true)
For example would produce:
<input checked="checked" id="A" name="A" type="radio" value="B" />
The documentation for this is here

In your previous query, you got this answer.
You asked the same query in it's comment section. The problem was that the second line of code was missing one parameter.
Please check below details....
Parameter Details
Razor Syntax
#Html.RadioButton("smth", "smth", true)
#Html.RadioButtonFor( m => m.Prop, true, new { id = "rdBtn" } )

Related

Razor broken when a space between HTML’s attribute and its value

I have a ViewBag like this
ViewBag.ApplyDiscount = false;
ViewBag.ExpressShip = true;
ViewBag.Supplier = null;
and some cshtml snippets like this
Discount:<input type="checkbox" checked="#ViewBag.ApplyDiscount"/>
Express:<input type="checkbox" checked="#ViewBag.ExpressShip"/>
Supplier:<input type="checkbox" checked="#ViewBag.Supplier"/>
after razor rendered the cshtml snippets,the real html will be
Discount:<input type="checkbox"/>
Express:<input type="checkbox" checked="checked"/>
Supplier:<input type="checkbox"/>
but if I add a space between the checked attribute and =like
Discount:<input type="checkbox" checked ="#ViewBag.ApplyDiscount"/>
Express:<input type="checkbox" checked ="#ViewBag.ExpressShip"/>
Supplier:<input type="checkbox" checked ="#ViewBag.Supplier"/>
razor will render the cshtml snippet in a wrong way. The html will be like this:
Discount:<input type="checkbox" checked ="False"/>
Express:<input type="checkbox" checked ="True"/>
Supplier:<input type="checkbox" checked =""/>
This will happen no matter MVC4(VS2012) or MVC5(VS2015).
So, can anyone tell me why a space will cause this thing to happen?
Because checked itself is a special stand-alone boolean attribute from legacy HTML and can be valid syntax without an attribute value.
It's presence alone indicates that the box is checked.
e.g.
<input type="checkbox" checked />
and
<input type="checkbox" checked="true" />
Perform the same way. It's why in jquery we always use the :checked selector. It obfuscates the need to check the variation.

How can I tweak Knockout's writeable computed observeables to work with MVC model binding?

I have a value for user display and a similar value for storage. How can I modify what I have so that I save the correct data to the model?
Fiddle
HTML
<div>formatted value for user display</div>
<input type="text" data-bind="value: formattedUnitOfCost" id="Model_Bound_ID" />
<div>unformatted value (the one I'd like to save)...this is not model bound</div>
<input type="text" data-bind="value: unitOfCost" />
JavaScript/Knockout
function AppViewModel() {
var self = this;
self.unitOfCost = ko.observable(1.01).extend({
isServerSet: false
});
self.formattedUnitOfCost = ko.computed({
read: function () {
return '$' + self.unitOfCost().toFixed(2);
},
write: function (value) {
// Strip out unwanted characters, parse as float, then write the raw data back to the underlying "price" observable
value = parseFloat(value.replace(/[^\.\d]/g, ""));
self.unitOfCost(isNaN(value) ? 0 : value); // Write to underlying storage
},
owner: self
});
}
ko.applyBindings(new AppViewModel());
The unformatted value is not displayed to the user. Model_Bound_ID is user editable.
You're doing it the wrong way.
Your model bound control (I mean the control that will be posted to your controller, and has the unformatted value) should be created like any other control, for example using Html.HiddenFor or whatever you want in your (Razor?) template. And you must add the data-bind attribute in the template. Remember that low-dash will be converted in medium-dash, so you can add it in the attributes parameter of the Html Helper like this: { data_bind = "value: unitOfcost" }.
Obviously this hidden field will be sent to the controller when posted (direct post, ajax, or whatever).
Now you need to add the visible control, and bind it to another observable. This observable will be a computed observable, which will do this:
on read, it will take the value from unitOfWork, and return it formatted
on write, it will parse the value to convert it to number, and update the unitOfWork observable with the parsed value
In fact you've got nearly all the code, but were implementing it all the way back.
Another way of looking at this answer is to change the html from this:
<input type="text" data-bind="value: formattedUnitOfCost" id="Model_Bound_ID" />
<input type="text" data-bind="value: unitOfCost" />
to this
<input type="text" data-bind="value: formattedUnitOfCost"/>
<input type="text" data-bind="value: unitOfCost" id="Model_Bound_ID" />
And KO does the rest by the power of the observables. Gosh I love KO

Display label before input using formRow view helper in Zf2

In Zend Framework 2.1.4 I am using the standard form view helpers to render out my form elements.
When I try:
<?php echo $this->formRow($form->get('Title'));?>
The label text and input element are placed within the label:
<label>
<span>Title</span><input type="text" name="Title" placeholder="Inserisci titolo"
required="required" value="">
</label>
The same with:
<?php echo $this->formCollection($form, TRUE);
However, if I render out the label and input individually:
echo $this->formLabel($form->get('Title'));
echo $this->formInput($form->get('Title'));
It generates the html I want:
<label for="Title">Title</label>
<input type="text" name="Title" placeholder="Insert Title" required="required" value="">
How can I achieve the same with the formRow view helper?
If a form element does not have an "id" attribute, the label will wrap the input:
<label>Label<input /></label>
Otherwise:
<label for="test">Label</label><input id="test" />
Looking at (zf2 version 2.25 dev):
\Zend\Form\View\Helper\FormRow
It appears that if you don't provide an id for your form elements, the default general behaviour is to place the input element inside their corresponding label element.
The second argument for the formRow view helper, will place the label text before (prepend) or after (append) the input element in the document flow. (The default is to place it before.)
Check the render method for more details.
In first you must look source code to understand how formRow works : https://github.com/zendframework/zf2/blob/master/library/Zend/Form/View/Helper/FormRow.php
After you'll see in this code that __invoke has $labelPosition parameter that you can prepend or append with const LABEL_APPEND and LABEL_PREPEND.
In short, try to do something like this :
$this->formRorw($form->get('element'), 'prepend'); // Or append as you want

Grails check checkbox with value same as variable

Hi I have this value that I passed to the gsp
${d.causalOrganism}
I have this three checkboxes and I want one of them to be checked based on the above value. How can I do so? I tried but doesn't work.
<label for="causalOrganism">Causal Organism:</label>
Fungi<input type="checkbox" value="Fungi" name="causalOrganism" id="causalOrganism1" onclick="checkOrganism(this.id)"/>
Bacteria<input type="checkbox" value="Bacteria" name="causalOrganism" id="causalOrganism2" onclick="checkOrganism(this.id)"/>
Virus<input type="checkbox" value="Virus" name="causalOrganism" id="causalOrganism3" onclick="checkOrganism(this.id)" />
Add the checked attribute to your checkbox based on your desired condition:
...
Fungi<input type="checkbox" value="Fungi" name="causalOrganism" id="causalOrganism1" onclick="checkOrganism(this.id)" ${(d.causalOrganism == 'causalOrganism1') ? "checked='checked'" : ''}/>
...
You should think about using the grails checkBox. With this you could use the checked attribute directly.

MVC-entering text into textbox with on click event

I have view for entering new entity. On that view I have two buttons: one for saving new entity and another button should enter value into textbox on that form but I can't achieve this, the value in the textbox stays same as it was on page load. Does anyone know how to do this?
Thx
There are two possibilities:
Use javascript
Reload the page with additional query string containing the new value
Example with jquery:
<input type="text" name="foo" id="foo" value="old value" />
<input type="button" id="update" value="Update value" />
and the script
$(function() {
$('#update').click(function() {
$('#foo').val('some new value');
});
});

Resources