aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Can't set custom 'value' attribute for a checkbox

Open dlr1 opened this issue 8 years ago • 16 comments

The following code in .html file works but not in a page (.cshtml)

<fieldset>
  <legend>Choose your interests</legend>
  <div>
    <input type="checkbox" id="coding" name="interest" value="coding" checked>
    <label for="coding">Coding</label>
  </div>
  <div>
    <input type="checkbox" id="music" name="interest" value="music">
    <label for="music">Music</label>
  </div>
</fieldset>

dlr1 avatar Apr 17 '18 13:04 dlr1

What exactly is the problem? what is the error?

floreseken avatar Apr 17 '18 13:04 floreseken

Coding Checkbox is not checked

dlr1 avatar Apr 17 '18 13:04 dlr1

I guess you should remove the value= stuff. What is the meaning of that?

floreseken avatar Apr 17 '18 13:04 floreseken

@floreseken value is used when passing form data to a backend. Don't know if @dlr1 is using it though, his example is just copied from MDN.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox

I think he/she needs to provide more of the page's code or no one will be able to help.

awulkan avatar Apr 17 '18 16:04 awulkan

I guess you should remove the value= stuff. What is the meaning of that?

@floreseken , value is a acceptable property for every input element, it's not the problem here.

He's question is about "checked" property that is not working as expected. If you create a

<input type="checkbox" checked />

It should be checked when you run on browser, and if you make it on blazor, are not checking. Take a look on above screen, first one is on Blazor page, the second one is on a pure html.

Image of coding

Edit: Even using checked="checked" not make checkbox selected.

brunopbarrote avatar Apr 17 '18 16:04 brunopbarrote

@brunopbarrote Thanks. I was trying to get a checkboxlist working and was not able to get that working anyway I tried. so like @AWulkan said, I copied html from that link to see if that would work.

dlr1 avatar Apr 17 '18 16:04 dlr1

The culprit is certainly value, but not sure why:

<input type="checkbox" checked /> Works
<input type="checkbox" value="coding" checked /> Does Not Work

Generated code:

base.BuildRenderTree(builder);
builder.OpenElement(7, "input");
builder.AddAttribute(8, "type", "checkbox");
builder.AddAttribute(9, "checked", "");
builder.CloseElement();
builder.AddContent(10, " Works\n            ");
builder.OpenElement(11, "input");
builder.AddAttribute(12, "type", "checkbox");
builder.AddAttribute(13, "value", "coding");
builder.AddAttribute(14, "checked", "");
builder.CloseElement();
builder.AddContent(15, " Does Not Work\n");

Edit:

And value is not actually rendering:

image

zbecknell avatar Apr 17 '18 17:04 zbecknell

I'm going to take a shot at tracking this down in a few hours.

zbecknell avatar Apr 17 '18 21:04 zbecknell

Ok, I still have a pretty shallow understanding of the codebase, but I've done some digging to discover what might be happening. I'm sure all of this is quite obvious for @SteveSandersonMS and @rynowak, and one of them will probably need to solve it.

Value Attribute Not Rendered

This happens because the value attribute is being treated specially for binding purposes. If you check out BrowserRenderer.ts you'll find this:

if (attributeName === 'value') {
  if (this.tryApplyValueProperty(toDomElement, renderTreeFrame.attributeValue(attributeFrame))) {
    return; // If this DOM element type has special 'value' handling, don't also write it as an attribute
  }
}

Checked State Not Reflected

Following the logic above, we can see that the checked state relies on the value attribute -- an empty string in the value attribute will cause the checkbox to render in the on state and a present string will render an off checkbox.

private tryApplyValueProperty(element: Element, value: string | null) {
  // Certain elements have built-in behaviour for their 'value' property
  switch (element.tagName) {
    case 'INPUT':
    case 'SELECT':
    case 'TEXTAREA':
      if (isCheckbox(element)) {
        (element as HTMLInputElement).checked = value === '';
      } else {
        (element as any).value = value;

So this:

<input type="checkbox" value="" /> Empty String
<br/>
<input type="checkbox" value="test" /> Present String 
<br/>
<input type="checkbox" checked /> No Value Attribute (checked attribute)
<br/>
<input type="checkbox" /> No Value Attribute (no checked attribute)

Will render this:

image

TL;DR: Some work needs to be done to support custom value attribute values on checkboxes.

zbecknell avatar Apr 18 '18 01:04 zbecknell

Yes, we have repurposed value here and there may be an argument that we shouldn't in this case. However I'm not sure what you'd usefully do by setting a custom value attribute on a checkbox in a Blazor app, since that would normally only be used for an HTML form post, which is something people don't reasonably do in a SPA.

We'll have to give this further considation. Adding to backlog.

SteveSandersonMS avatar Apr 20 '18 17:04 SteveSandersonMS

"Yes, we have repurposed value here and there may be an argument that we shouldn't in this case."

I don't think default attributes should be repurpsed under any circumstance, unless it's really a dealbreaker if they are not.

For example in Vue you still have the default attributes, but then you can hook them up with vue by typing a colon infront of them, like :value="myValue" or :data-something="something".

awulkan avatar Apr 22 '18 14:04 awulkan

While this is a bug, a current workaround is to do something like this: <input type="checkbox" id="coding" name="interest" bind=@coding>

This sets your coding variable to be bound to the input... i.e. when you click it, it updates your coding boolean variable... you can see this by putting @coding in the html page, like so: <input type="checkbox" id="coding" name="interest" bind=@coding> @coding

KnottStudios avatar May 29 '18 18:05 KnottStudios

Because "value" is being repurposed (which is a bad idea in my opinion) you cannot do the <input value=@MyProperty onchange=@(eventArgs => { MyProperty = eventArgs.NewValue; DoSomethingElse(); }) /> @SteveSandersonMS suggestion (here) to have both a value and an onchange. There are also suggestions that we use the @bind and then do some kind of model pollution in the properties setters to fire your DoSomethingOnChange method. This works for simple models, but not good for lists of things.

chadrickm avatar Oct 31 '19 11:10 chadrickm

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Oct 21 '21 22:10 ghost

A related issue I believe... Note: using MVC pattern with razor views (not pages) and the built in tag helpers.

Using the input tag helper with a type of checkbox, a custom value can be set and bind back to a string property successfully (although determining if the checkbox is checked isn't handled quite right). The issue comes if there are any model errors, in which case we return the form with the errors, however this throws an exception that the custom checkbox value can't be converted to boolean.

Steps to Reproduce

class Form
{
    [EmailAddress]
    public string Email { get; set; }

    public string Subscribe { get; set; }    
}
<form method="post">
    <input asp-for="Email" type="text" />
    <input asp-for="Subscribe" type="checkbox" value="newsletter" />
</form>
public IActionResult ControllerAction(Form form)
{
    // Assume an invalid email was entered for this example, or any other model validation errors
    if (!ModelState.IsValid) return View(form);
}

Produces...

InvalidOperationException: Unexpected expression result value 'newsletter' for asp-for. 'newsletter' cannot be parsed as a 'System.Boolean'.
Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper.GenerateCheckBox(ModelExplorer modelExplorer, TagHelperOutput output, IDictionary htmlAttributes)
Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper.Process(TagHelperContext context, TagHelperOutput output)
Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.RunAsync(TagHelperExecutionContext executionContext)

This error only occurs after submitting and having model state values present when trying to render the view.

I've traced the issue to the DefaultHtmlGenerator.GetModelStateValue() method which attempts to convert the value to a boolean only if it is in the model state.

internal static object GetModelStateValue(ViewContext viewContext, string key, Type destinationType)
{
    if (viewContext.ViewData.ModelState.TryGetValue(key, out var entry) && entry.RawValue != null)
    {
        return ModelBindingHelper.ConvertTo(entry.RawValue, destinationType, culture: null);
    }

    return null;
} 

c3hb avatar Feb 17 '23 23:02 c3hb

A related issue I believe... Note: using MVC pattern with razor views (not pages) and the built in tag helpers.

Using the input tag helper with a type of checkbox, a custom value can be set and bind back to a string property successfully (although determining if the checkbox is checked isn't handled quite right). The issue comes if there are any model errors, in which case we return the form with the errors, however this throws an exception that the custom checkbox value can't be converted to boolean.

Steps to Reproduce

class Form
{
    [EmailAddress]
    public string Email { get; set; }

    public string Subscribe { get; set; }    
}
<form method="post">
    <input asp-for="Email" type="text" />
    <input asp-for="Subscribe" type="checkbox" value="newsletter" />
</form>
public IActionResult ControllerAction(Form form)
{
    // Assume an invalid email was entered for this example, or any other model validation errors
    if (!ModelState.IsValid) return View(form);
}

Produces...

InvalidOperationException: Unexpected expression result value 'newsletter' for asp-for. 'newsletter' cannot be parsed as a 'System.Boolean'. Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper.GenerateCheckBox(ModelExplorer modelExplorer, TagHelperOutput output, IDictionary htmlAttributes) Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper.Process(TagHelperContext context, TagHelperOutput output) Microsoft.AspNetCore.Razor.TagHelpers.TagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output) Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.RunAsync(TagHelperExecutionContext executionContext)

This error only occurs after submitting and having model state values present when trying to render the view.

I've traced the issue to the DefaultHtmlGenerator.GetModelStateValue() method which attempts to convert the value to a boolean only if it is in the model state.

internal static object GetModelStateValue(ViewContext viewContext, string key, Type destinationType)
{
    if (viewContext.ViewData.ModelState.TryGetValue(key, out var entry) && entry.RawValue != null)
    {
        return ModelBindingHelper.ConvertTo(entry.RawValue, destinationType, culture: null);
    }

    return null;
} 

For anyone interested, a fix that I used to deal with this exact issue is to remove the offending element from the ViewData. Example: ModelState.Remove("OffendingElementKeyHere");

RebelTank avatar Apr 03 '24 22:04 RebelTank