@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>
此脚本位于cshtml razor视图中,进入视图模型为null,但我得到一个空引用异常:
异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。
Source Error:
Line 20: {
Line 21: Response.Write("inittext = '@Model.MessageText';");
Line 22: }
Line 23: return inittext;
Line 24: }
源文件:d:\ Projects \ Patient Portal \ Portal.MVC \ Views \ Message \ Compose.cshtml Line:22
为什么即使if条件为false,仍然会执行response.write,为什么异常指向第22行而不是第21行?
答案 0 :(得分:1)
我没有MVC5来测试这个,但我认为你的Response
对象是null,你应该创建这样的动态javascript:
@if (Model != null)
{
<text>inittext = '@Model.MessageText';</text>
}
此外,请确保Model.MessageText
正确转义,以便其内容不会导致javascript错误。
答案 1 :(得分:0)
由于您处于JS代码块中,因此您需要使用显式CSHTML开始标记来执行if。