I am encoding an MVC3 application using ajax and I have a situation.
I should show na Button only if the condition is true. Well, that is easy to do.
@if (Model.AAA == ENUM.AAA) { <button>OK</button> }
but this button will call the ajax function.
Now I doubt WHERE PLACE MY AJAX CODE?
if i do this:
@if (Model.AAA == ENUM.AAA) { function OK(){ $.ajax({}); } <button>OK</button> }
The ugly code sounds !!! He sees that the code "is not in the right place, and the ajax code is" safe ", I mean, the ajax code will exist only if the button exists.
but if I put my code in the chapter section, an advanced user will be able to call the ajax function.
or if you make an @if clause to enclose a script, I will duplicate code like this
<head type="text/javascript"> @if (Model.AAA == ENUM.AAA){ function OK(){ $.ajax({}); } } </head> .... <body> .... @if (Model.AAA == ENUM.AAA) { <button onclick="OK()">OK</button> } .... </body>
So, What is the best practice to solve this situation, the best approach?
source share