locked
check checkbox value inside the value attribute RRS feed

  • Question

  • User485318798 posted

    Hi,

    ist it possible to check inside the value attribute if a checkbox is checked and then pass the value to a model.

    For example:

    <input type="checkbox" name="checkText" id="checkText" value="If checkbox checked than ->" ? @(Model.TextCheck.ElementAt(i) = true : Model.TextCheck.ElementAt(i) = false)" />

    Is this possible?

    Thank you

    Tuesday, September 15, 2015 10:37 AM

Answers

  • User281315223 posted

    I get the error Message:

    'bool' does not contain a definition for 'Checked' and no extension method 'Checked' accepting a first argument of type 'bool' could be found


    for: @(Model.TextCheck.ElementAt(i).Checked

    Sorry that was from my previous code when I wasn't sure what you were passing in. Since you are using a boolean value, you would just use :

    <input type="checkbox" name="checkCopy" value='@Model.TextCheck.ElementAt(i)' @(Model.TextCheck.ElementAt(i) ? "checked='checked'" : "") />

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 21, 2015 8:38 AM

All replies

  • User281315223 posted

    By default, only checkboxes that are checked will be posted to the server (or your specific model). So you should be able to set your value regardless and it will either post the value (if the element is checked) or it won't be sent to the server at all.

    Wednesday, September 16, 2015 8:23 AM
  • User485318798 posted

    Do you mean:

    <input type="checkbox" name="checkCopy" id="checkCopy" @(Model.TextCheck.ElementAt(i) ? "checked = \"checked\"" : "") />

    I tried this but it doesn't work. I am inside a for loop and want for each Element in the TextCheck List to save the checkbox value

    Thursday, September 17, 2015 2:26 AM
  • User281315223 posted

    I tried this but it doesn't work. I am inside a for loop and want for each Element in the TextCheck List to save the checkbox value

    One of the problems with using something like this exactly within a loop is that you are going to be creating multiple elements with the same ID, which would be invalid markup and could cause all sorts of bizarre issues. If you are looping through like this, you might also want to set a value as well :

    <input type="checkbox" name="checkCopy" @(Model.TextCheck.ElementAt(i).Checked ? String.Format("checked='checked' value='{0}'",Model.TextCheck.ElementAt(i).Value) : "") />

    I'm not sure what your actual TextCheck class / model looks like, but you would probably have some type of property to indicate it was checked along with a value as seen above.

    Thursday, September 17, 2015 8:23 AM
  • User485318798 posted

    TextCheck is a List<bool>. I want to save the checkbox values inside the List. So if first checkbox is checked, second not checked, then The first element in the TextBox should be true and the second false

    Thursday, September 17, 2015 8:49 AM
  • User281315223 posted

    So you want to actually save the values of "true" or false" in the list based on if an element is checked or not?

    <input type="checkbox" name="checkCopy" value='@Model.TextCheck.ElementAt(i)' @(Model.TextCheck.ElementAt(i).Checked ? "checked='checked'" : "") />

    Using your example, this should output two checkboxes, one with an initial value of "true" and the second being a value of "false" (with the first one being checked).

    Thursday, September 17, 2015 11:51 AM
  • User485318798 posted

    I get the error Message:

    'bool' does not contain a definition for 'Checked' and no extension method 'Checked' accepting a first argument of type 'bool' could be found


    for: @(Model.TextCheck.ElementAt(i).Checked

    Monday, September 21, 2015 4:50 AM
  • User281315223 posted

    I get the error Message:

    'bool' does not contain a definition for 'Checked' and no extension method 'Checked' accepting a first argument of type 'bool' could be found


    for: @(Model.TextCheck.ElementAt(i).Checked

    Sorry that was from my previous code when I wasn't sure what you were passing in. Since you are using a boolean value, you would just use :

    <input type="checkbox" name="checkCopy" value='@Model.TextCheck.ElementAt(i)' @(Model.TextCheck.ElementAt(i) ? "checked='checked'" : "") />

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 21, 2015 8:38 AM