我在做NerdDinner。我想使用Razor sytax而不是Web表单。当我调试项目时。它说“模型”没有定义。整个显示的错误如下:
Unhandled exception at line 133, column 9 in http://localhost:52372/Dinner/Create
0x800a1391 - Microsoft JScript runtime error: 'Model' is undefined
它生成了一个名为
的文件Create[dynamic]
文件的含义是什么,我的Razor语法有什么问题。
提前致谢。
原始语法
<script src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2" type="text/javascript"></script>
<script src="/Scripts/Map.js" type="text/javascript"></script>
<div id="theMap">
</div>
<script type="text/javascript">
$(document).ready(function() {
var latitude = <%=Model.Latitude%>;
var longitude = <%=Model.Longitude%>;
if ((latitude == 0) || (longitude == 0))
LoadMap();
else
LoadMap(latitude, longitude, mapLoaded);
});
function mapLoaded() {
var title = "<%=Html.Encode(Model.Title) %>";
var address = "<%=Html.Encode(Model.Address) %>";
LoadPin(center, title, address);
map.SetZoomLevel(14);
}
</script>
Razor语法
@model NerdDinner.Models.Dinner
<script src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2" type="text/javascript"></script>
<script src="/Scripts/Map.js" type="text/javascript"></script>
<div id="theMap">
</div>
<script type="text/javascript">
$(document).ready(function () {
var latitude = Model.Latitude;
var longitude =Model.Longitude;
if ((latitude == 0) || (longitude == 0))
LoadMap();
else
LoadMap(latitude, longitude, mapLoad);
});
function mapLoad()
{
var title = "@Html.Encode(Model.Title)"
var address = "@Html.Encode(Model.Address)"
LoadPin(center,title,address);
map.setZoomLevel(14);
}
</script>
答案 0 :(得分:1)
var latitude = Model.Latitude;
var longitude =Model.Longitude;
应该是:
var latitude = @Html.Raw(Json.Encode(Model.Latitude));
var longitude = @Html.Raw(Model.Longitude));
也是这样:
var title = "@Html.Encode(Model.Title)"
var address = "@Html.Encode(Model.Address)"
应该是:
var title = @Html.Raw(Json.Encode(Model.Title));
var address = @Html.Raw(Json.Encode(Model.Address));
答案 1 :(得分:0)
试试这个:
var latitude = @Model.Latitude;
var longitude = @Model.Longitude;