asp.net mvc and valid xhtml? - asp.net-mvc

For some reason an html helper is outputting this html which doesnt validate.
the validator tells me
There is no attribute "Length"
<%= Html.CheckBox("Medicamentos", Model.Medicamentos) %>
is outputting
<input type="checkbox" value="true" name="Medicamentos" id="Medicamentos" checked="checked" length="4">

I assume that it's matching the signature that takes a string and an object since I don't know what Model.Medicamentos is. In that case it takes the properties of the object and turns them into attributes on the element. I suspect that you simply want to use the Checked attribute on the Model property specified as the default value of the checkbox, i.e.,
<%= Html.CheckBox( "Medicamentos", Model.Medicamentos.Checked ) %>
In, which case, assuming that Checked is boolean it will match the correct method signature on the helper extension.

Related

Getting Params from raw HTML components in Rails

I'm making an input form whose features don't fit so nicely into the general form_for template rails provides. I figured I would write my own HTML mimicking the html output of form_for, but embedded with other form_for elements.
For fields I could use the rails framework for I did. For the others I made hidden fields to store what was going to Rails, and regular input fields whose values I manipulated with JavaScript to put into the hidden fields.
This is one such field:
State:<br>
<input type="text" class = "state name_input administrative_area_level_1">
<div class="field">
<input type="hidden" name="address[state]" id="state">
</div>
When I send the value of the hidden field to the console, I get a good response:
state 37
Which means the state field holds the value 37. Perfect.
Except that when I try to make my model, the params come in empty:
"state"=>"",
I do have one field that works that isn't hidden:
Street:<br><input type="text" id="street" name="address[street]">
So I changed the state input type to number, which is what it would be if it weren't hidden, but same result. Why can't rails find my param?
You can show post more detail. I can't understand 100% things you do because you write pretty simple. But i guess that you need test params name is address[state] but it's not only state becase if only state of course it nil.
if html of you rendered same up then i suggest you should put one debug in controller to see params return controller when submit form.
For example: in controller you add line:
logger.debug params
to see right params that form send.
If your form is not tied directly to a object model, it might be difficult to use the form_for helper. In those cases you should use form_tag helper. Read more about form_tag helper here.
Refer this stackoverflow answer for the difference between form_for and form_tag helpers.
Hope this helps.

Ruby on Rails checkbox not saving data

I have a checkbox
<%= f.check_box :anonymous %>
And my table has a column anonymous which is true or false.
Code generated in html:
<input name="comment[anonymous]" type="hidden" value="0" />
<input id="comment_anonymous" name="comment[anonymous]" type="checkbox" value="1" />
Now, for some reason when I add data it's not saving if my anonymous checkbox is checked or not.. it's not changing data in database.. All other fields gets saved except anonymous.
What can be the problem ?
Use #check_box_tag instead:
<%= check_box_tag(:anonymous) %>
From the official guides:
Array parameters do not play well with the check_box helper. According
to the HTML specification unchecked checkboxes submit no value.
However it is often convenient for a checkbox to always submit a
value. The check_box helper fakes this by creating an auxiliary hidden
input with the same name. If the checkbox is unchecked only the hidden
input is submitted and if it is checked then both are submitted but
the value submitted by the checkbox takes precedence. When working
with array parameters this duplicate submission will confuse Rails
since duplicate input names are how it decides when to start a new
array element. It is preferable to either use check_box_tag or to use
hashes instead of arrays.

ASP.Net MVC 2 - How to stop the view from using prefixes on fields?

ASP.Net Html.TextBoxFor (and the other XxxxxFor editor helper methods) default to rendering a field prefix. How do I disable field prefixes so it simply renders the property name as the Name/ID?
Here's an example:
<%= Html.EditorFor(m => chart.Title) %>
is rendered as:
<input id="Chart_Title" name="Chart.Title" type="text" value="">
I would prefer it to be rendered as:
<input id="Title" name="Title" type="text" value="">
There's a parameter in an overload of EditorFor called htmlFieldName. Specify your name here.
The helpers take whatever you give it. So you need to pass in just the "Title" portion of the variable; maybe try assigning title to a variable, and use Html.EditorFor for that Title variable, and that might give you a different response.
The reason it does that is the helpers are setup to reflect from the Model context, so typically you may see Model.Chart.Title, and as such, the helpers create this path in the name so it can post the entire model back to the action method, if it needed to.
HTH.

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"})

In MVC 2, how to bind a Html.TextBox to a Model property

My model has a property of "output" and my form has a TextBox named "output", both spelled exactly the same. When I fill out the form and post it, the returning view has unexpected results.
Specifically, I receive the posted "output" form variable which is then bound to my Model in the Controller Method, then I change this variable to a different value before I pass the Model back to the view.
the posted output property is "one thing"
my Model property is set to "another"
I have this in my code: <%= Html.TextBox("output") %>
Which renders to this: <input id="output" name="output" type="text" value="one thing" />
However, when debugging, the Model does contain the correct value ("another").
Its using the posted value rather than the value assigned in the controller. Any ideas why and how to fix it?
Assuming you're using a strongly typed view like so:
<%# Page ... Inherits="System.Web.Mvc.ViewPage<...TestModel>" %>
And having an EditorFor replacing your
<%= Html.TextBox("output") %> like so <%= Html.EditorFor(p=>p.output) %>
you can do in your Controller
ModelState.SetModelValue("output", new ValueProviderResult("Some string", string.Empty, new CultureInfo("en-US")));
A similar question has been asked here

Resources