@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?
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.
Comments