0

I have a drop down list and two textboxes on an ASP.Net MVC 3 view. The drop down lists numbers from 1 to 10. The two textboxes are both wired up to use the JQuery Datepicker.

When the user selects an option from the drop down, I would like JQuery code to clear the values in the two textboxes.

I have written the JQuery code below which does just this, however, when I go to submit the form details to the post method within my controller, the dates I have selected for the two textboxes are cleared.

This is my code to date:

$(document).ready(function () {


$('#equipment_warrantyLength').change(ResetDates); //fires when drop down changed

function ResetDates() {
    alert("hello");

    $("#equipment_purchaseDate").val(''); // clear textbox
    $('#equipment_warrentyExpires').val(''); //clear textbox

}

});

The textboxes should only be cleared when the drop down is selected, but currently, they also clear when the user attempts to submit the form.

Any feedback would be much appreciated.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
tcode
  • 5,055
  • 19
  • 65
  • 124

1 Answers1

2

When the form is submitted to the server the page is reloaded. Unless you set the submitted values in the textboxes upon loading the page again they are cleared. ASP.NET MVC doesn't use Viewstate like ASP.NET WebForms so unless you manually init the fields they will be empty when pages are reloaded after a form submission.

Update:

You can also submit the form data via jQuery. In order to do that you would do something like this:

  1. Add a click handler to the submit button. (If it is declared as you should also change it to a normal button so that the browser wont submit the form automatically.
  2. In the click handler add code to do the ajax submission. See this question for tips on that Submit form using AJAX and jQuery

But en the end it might be easier to just put the submitted data in the viewmodel and reinitialize the fields in the view after submission.

Community
  • 1
  • 1
Samuel Otter
  • 608
  • 3
  • 7
  • Ok. Is there any way I can write some JQuery code which doesnt fire my ResetDates() function when the form is being submitted? – tcode Feb 22 '11 at 15:14
  • updated my reply with some pointers. I can't give much more detail without knowing more about the actual form etc. – Samuel Otter Feb 22 '11 at 16:08