[RESOLVED]Checkboxfor is returning true even when its false in mvc 4 razor.

Hello,

I am try to change the checkboxfor helper from javascript like below:

function toggleClass(RowID) {               
        if (document.getElementById('buttonSuccess' + (RowID + 1).toString()).className == "btn btn-success") {
            document.getElementById('buttonSuccess' + (RowID + 1).toString()).className = "btn btn-danger";            
            document.getElementById('chkSuccess' + (RowID + 1).toString()).checked = false;          
        }
        else {
            document.getElementById('buttonSuccess' + (RowID + 1).toString()).className = "btn btn-success";
            document.getElementById('chkSuccess' + (RowID + 1).toString()).checked = true;
        }       
        return false;
    }

And my checkboxfor syntax is as follows:

@Html.CheckBoxFor(model => model.AuxiliaryProgramMappingDTO[i].IsActive, new { id="chkSuccess"+Model.AuxiliaryProgramMappingDTO[i].BreakTypeid.ToString(),style="display:none"})

Problem is when i try to change the state of checkbox from unchecked to checked or vice versa from javascript , its not reflecting back to the model.

Any help please. :)

You either need to submit the form, e.g. document.forms[0].submit() or use hidden field because the model (

@Model

) only exists while the page is being constructed.  

See example here http://stackoverflow.com/questions/16174465/how-do-i-update-a-model-value-in-javascript-in-a-razor-view 

smirnov

You either need to submit the form,

Sorry , but didnt get u at this part , why shall i post the form , if i do so then all my values will get submitted which will be incomplete.Can you elaborate a bit about this ?

Ashim Chatterjee

@Html.CheckBoxFor(model => model.AuxiliaryProgramMappingDTO[i].IsActive, new { id="chkSuccess"+Model.AuxiliaryProgramMappingDTO[i].BreakTypeid.ToString(),style="display:none"})

how are the ids and mames rendering for these checkbox elements?

Dear cnuonline,

I solved this issue by using hiddenfieldfor ..and changing the value property of the same from javascript.This results in changing the flag for true to false and vice versa.

Leave a Reply