Statement inside If block executing even when condition is false

BlackICE
@model Portal.Models.MessageModel

<link href="~/Content/jquery.wysiwyg.css" rel="stylesheet" />
<script type="text/javascript" src="~/Scripts/jquery-2.0.3.js"></script>
<script src="~/Scripts/jquery-migrate-1.2.1.js"></script>
<script src="~/scripts/jquery.unobtrusive-ajax.js"></script>
<script type="text/javascript" src="~/Scripts/jquery.wysiwyg.js"></script>
<script type="text/javascript" src="~/Scripts/wysiwyg-controls/wysiwyg.image.js"></script>
<script type="text/javascript" src="~/Scripts/wysiwyg-controls/wysiwyg.link.js"></script>
<script type="text/javascript" src="~/Scripts/wysiwyg-controls/wysiwyg.table.js"></script>
<script type="text/javascript" src="~/Scripts/wysiwyg-controls/wysiwyg.cssWrap.js"></script>
<script type="text/javascript" src="~/Scripts/wysiwyg-controls/wysiwyg.colorpicker.js"></script>

<script type="text/javascript">
    (function($) {
        $(document).ready(function() {
            $('#messagearea').wysiwyg({
                initialContent: function () {
                    var inittext = "";
                    @{if (Model != null)
                       {
                           <text>inittext = '@Model.MessageText';</text>
                       }
                    }
                        return inittext;
                }
            });
        });
    })(jQuery);
</script>
    <table id="compose-table" style="border-spacing:0 0; max-width: 1200px; min-width: 450px;box-shadow: 0 0 5px 2px #cfcfcf; width:auto; background-color:white">
        <tbody>
            <tr >
                <td style="box-shadow: inset 0 0 10px #383838;">
                    <form>
                        <div id="destinations" style="border-bottom: 1px solid #cfcfcf; padding: 12px 1px 12px 2px; ">                            

                            **<input name="to" spellcheck="false" autocomplete="false" autocapitalize="false" autocorrect="off" style="padding-left: 8px; background-color:transparent; width:1078px; resize:none; border: none;" placeholder="To" onfocus="displayCCandBCC" value="@Model.From"/>**
                        </div>
                        <div id="addressdiv" style="display: none">
                            <table>
                                <tbody>
                                    <tr id="cc" style="display: none"></tr>
                                    <tr id="bcc" style="display: none"></tr>
                                </tbody>
                            </table>
                        </div>

                        <div id="subjectdiv" style="border-bottom: 1px solid #cfcfcf; padding : 12px 1px 12px 2px; ">
                        @{if (Model == null){
                            <input name="subject" style="padding-left: 8px; width: 1078px; border: none; background-color: inherit" placeholder="Subject" />
                          }
                          else
                          {
                            <input name="subject" style="padding-left: 8px; width: 1078px; border: none; background-color: inherit" placeholder="Subject" value="@String.Concat("re: ", Model.Subject)" />
                          }
}
                        </div>
                        <textarea id="messagearea" style="min-height:300px; width:100%; padding:0 0 0 0"></textarea>
                    </form>
                </td>
            </tr>
        </tbody>
</table>

This script is in a cshtml razor view, coming into the view Model is null, but I get a null reference exception:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 20:       {
Line 21:           Response.Write("inittext = '@Model.MessageText';");
Line 22:       }
Line 23:       return inittext;
Line 24:   }

Source File: d:\Projects\Patient Portal\Portal.MVC\Views\Message\Compose.cshtml Line: 22

Why is the response.write still being executed even though the if condition is false, and why does the exception point to line 22 and not line 21?

David

I don't have MVC5 to test this, but I think your Response object is null, you should be creating your dynamic javascript like this instead:

@if (Model != null)
{
    <text>inittext = '@Model.MessageText';</text>
}

Also, make sure Model.MessageText is escaped properly so that it's contents don't cause a javascript error.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Else Part is not executing even when condition is false Laravel 5.2

From Dev

Executing statement inside if block is not printing in python

From Dev

if statement executing when it is either false or true

From Dev

Bash: 'if' statement always seems to evaluate first condition as true even when false

From Dev

Catch block executing even when try block throws no exception?

From Dev

When condition inside if statement is fractional

From Dev

Avoid executing `with` statement when nested inside identical `with`

From Dev

Python: Even if the the if statement equals false it still executes the code inside the if statement

From Dev

Functions returns false even when the if statement is true

From Dev

If Statement Still Executes when Condition is False

From Dev

Executing block inside another block

From Dev

IF EXISTS command appears to be executing, even when condition doesn't exist?

From Dev

conditional nested for loop executing code even when condition not satisfied

From Dev

IF EXISTS command appears to be executing, even when condition doesn't exist?

From Dev

Should a function inside an if block be typeof=='function' if the if condition is false?

From Dev

While Loop executing one last time when condition is set to false

From Dev

List Value gets updated inside an if statement even though the condition is not true

From Dev

foreach inside IF condition will not output, even when condition is TRUE

From Dev

Executing for loop inside if statement

From Dev

How is a local variable created even when IF condition evaluates to false in Ruby?

From Dev

Query returning data even when statement is False in codeigniter

From Dev

What condition was false in the if statement

From Dev

If statement second condition always executing

From Java

If (false == true) executes block when throwing exception is inside

From Dev

Javascript enters if statement when the condition should evaluate false

From Dev

Parse error on input 'if' when trying to use a condition inside a do block

From Dev

Return statement doesn't break out of block, even though condition is true

From Dev

Return statement doesn't break out of block, even though condition is true

From Dev

Merge statement inside if condition

Related Related

  1. 1

    Else Part is not executing even when condition is false Laravel 5.2

  2. 2

    Executing statement inside if block is not printing in python

  3. 3

    if statement executing when it is either false or true

  4. 4

    Bash: 'if' statement always seems to evaluate first condition as true even when false

  5. 5

    Catch block executing even when try block throws no exception?

  6. 6

    When condition inside if statement is fractional

  7. 7

    Avoid executing `with` statement when nested inside identical `with`

  8. 8

    Python: Even if the the if statement equals false it still executes the code inside the if statement

  9. 9

    Functions returns false even when the if statement is true

  10. 10

    If Statement Still Executes when Condition is False

  11. 11

    Executing block inside another block

  12. 12

    IF EXISTS command appears to be executing, even when condition doesn't exist?

  13. 13

    conditional nested for loop executing code even when condition not satisfied

  14. 14

    IF EXISTS command appears to be executing, even when condition doesn't exist?

  15. 15

    Should a function inside an if block be typeof=='function' if the if condition is false?

  16. 16

    While Loop executing one last time when condition is set to false

  17. 17

    List Value gets updated inside an if statement even though the condition is not true

  18. 18

    foreach inside IF condition will not output, even when condition is TRUE

  19. 19

    Executing for loop inside if statement

  20. 20

    How is a local variable created even when IF condition evaluates to false in Ruby?

  21. 21

    Query returning data even when statement is False in codeigniter

  22. 22

    What condition was false in the if statement

  23. 23

    If statement second condition always executing

  24. 24

    If (false == true) executes block when throwing exception is inside

  25. 25

    Javascript enters if statement when the condition should evaluate false

  26. 26

    Parse error on input 'if' when trying to use a condition inside a do block

  27. 27

    Return statement doesn't break out of block, even though condition is true

  28. 28

    Return statement doesn't break out of block, even though condition is true

  29. 29

    Merge statement inside if condition

HotTag

Archive