Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In our ASP.NET MVC 4 app we have an Index.cshtml view with a single form tag and multiple partial views embedded inside that tag. Index.cshtml has horizontal tabs on top and each partial view is associated with one of these tabs:

@{
    ViewBag.Title = "Main Page";
}
@section Scripts{
<script type="text/javascript">
   $(document).ready(function () {
     $('#btnClearAll').click(function () {
        $(':input').not(':button, :submit, :reset, :hidden').val('');
     });
});
</script>
}
<form action="#">
<div id="tabs">
  <ul>
     <li><a href="#Tab1" id="Tab1_menu_link">Tab 1/a></li>
     <li><a href="#Tab2" id="Tab2_menu_link">A</a></li>
     <li>.......</li> //other tabs ...
  </ul>
  <div id="Tab1">
     @{Html.RenderPartial("View1");}
  </div>
  <div id="Tab2">
     @{Html.RenderPartial("View2");}
  </div>
  ......//other embedded views etc.
</form>

View1 has a 'Clear All' button. We wrote the click event (as shown above) inside the Index.chtml file's tag so that when user clicks on the above button, it would clear the data from all the input controls from all the partial views inside this single form. But the button clears the data only from the controls inside partial view1.

The input controls of all the partial views are of type text (text boxes) some of their values are readonly and are coming from text boxes from other partial views. For instance, textbox1 of view1 is readOnly and have its value set to the textbox2 of view2. The values of all the non-readonly textboxes are are entered by the user.

How can we make the above code clear the data from input controls inside all the partial views?

Thanks..Nam

share|improve this question
    
What does View1 and View2 look like? –  Jasen Dec 21 '13 at 0:08
    
no reason it shouldn't work as is –  charlietfl Dec 21 '13 at 0:21
    
Jasen, I have modified my original post with further clarification. Please see the second last paragraph. –  nam Dec 21 '13 at 18:24
    
It would be far more helpful to others if we could see your views; truncate the code if there's lots of similar inputs. As it was mentioned above the javascript should work so there may be a problem with the html. Also, it may be a transcription error, but you are missing a closing script tag. –  Jasen Dec 21 '13 at 20:46
    
Jasen, I just added the closing script tag above. It's in the original code. The partial views are simple html files with input tags of type text. –  nam Dec 22 '13 at 0:38
add comment

1 Answer

  1. You should write this piece of JavaScript code in the bottom of the index.cshtml, instead of in the middle between PartialView1 and PartialView2.

  2. Make sure you included JQuery js file

  3. Load all the Partial Views before the script is rendered.

share|improve this answer
    
Albus, I've modified my original post. Please see the code in Index.cshtml file and further explanation in the second last paragraph. –  nam Dec 21 '13 at 18:29
    
Albus, all the related JQuery files are added (the code is working for the partial view1 where the btnClearAll button is). Moreover. all the partial views are loaded before the code runs as the code is included inside the $(document).ready() event as shown in my post above. –  nam Dec 22 '13 at 0:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.