MVC3 Razor - Javascript statements with nested server ifs
Why doesn't the razor template engine like:
@if (Model.ActionComplete) {
<script type="text/javascript">
@if (Model.Message.IsString()) {
}
</script>
}
It is complaining that the Model
in Model.Message
should be a constant and that the </script>
tag is missing an @end statement.
I am trying to get to:
@if (Model.ActionComplete) {
<script type="text/javascript">
$(document开发者_Python百科).ready(function () {
$("#user-form").slideUp();
@if (Model.Message.IsString()) {
@:showMessageBar("@(Model.Message)");
}
});
</script>
}
Razor doesnt like tags inside of a script block so its causing your parsing issue. If you really want the code there, then just ignore the warning.
for giggles, rename 'script' to textarea and see if it works.
Your syntax looks fine. Could you confir if at runtime the page produces what you expect? The IntelliSense engine for Razor v1 still has some quirks, especially if you have C# inside of JavaScript inside of HTML.
精彩评论