As the title suggests, I am trying to essentially copy values from one textbox to another after a checkbox is clicked. I know how to do this via javascript, the only problem is it needs to be done server-side with razor. I am trying to accomplish this with the code below however, it has been giving me the infamous “object reference not set to an instance of an object.” error. But I’m not exactly sure what it’s refering to, any help would be appreciated.
<div class="editor-field">
@Html.CheckBoxFor(model => model.SameAddress, new { id = "chkAddress" })
@{bool isChecked = false;
if (Boolean.TryParse(Request.Form.GetValues("chkAddress")[1], out isChecked) == true)
{
}
}
</div>
Razor is not going to help you in this. There are 2 ways to do this.
1) Use javascript : Listen for the checkbox change event and if it is checked, read the content of first text box and paste it in second. This is the preferred i would go for.
Some thing like this
Assuming you want to copy from textbox with id
address1to textbox with idaddress2when checkbox with idchkCopyis clickedworking sample : http://jsfiddle.net/9fM89/4/
2) Using a formpost : Let the user clicks on “Copy” button and it does a form submit to your action method where you read the values of those form elements and set it on those properties meant for the second check box and return that to the view.