【发布时间】:2020-09-21 05:42:39
【问题描述】:
ASP.NET MVC 3 的新手。从教程开始,我正在为我自己的模型镜像代码。我可以在脚本中访问我的 Index.cshtml 文件中的模型数据,但不能在我的 javascript 函数中访问。
我的模特
using System.ComponentModel.DataAnnotations;
namespace DotNetCoreSqlDb.Models
{
public class Glassblower
{
public int ID { get; set; }
public string TeacherID { get; set; }
public string Lat { get; set; }
public string Lng { get; set; }
public string Name { get; set; }
public string Bio { get; set; }
public string Url { get; set; }
public string Coe { get; set; }
public string SubscriptionLevel { get; set; }
public DateTime StartDate { get; set; }
}
}
我的控制器索引方法:
// GET: Glassblowers
public async Task<IActionResult> Index()
{
return View(await _context.Glassblower.ToListAsync());
}
我的索引.cshtml
@model IEnumerable<DotNetCoreSqlDb.Models.Glassblower>
@{
ViewData["Title"] = "Index";
}
<script id="gmapsrc" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBSJAns0GMR5gK60kw2c3IAnouZpyu_QHw"
></script>
<div class="row map-and-readout-container">
<div id="timescale-map" class="timescale-map"></div>
<div id="readout" class="readout">
@foreach (var item in Model) {
<div>
@Html.DisplayFor(modelItem => item.Name)
</div>
}
</div>
</div>
<script>
var model = @Html.Raw(Json.Encode(@Model));
</script>
目前我能够在页面上呈现项目名称。我的目标是在我的标签中的 javascript 函数中循环遍历模型。
我试过用
访问它var model = @Html.Raw(Json.Encode(@Model))
但是当我在 Visual Studio Mac 中单击运行时,我得到了这个构建错误
'IJsonHelper' does not contain a definition for 'Encode' and no accessible extension method 'Encode' accepting a first argument of type 'IJsonHelper' could be found (are you missing a using directive or an assembly reference?) (CS1061) (DotNetCoreSqlDb)
任何提示将不胜感激。谢谢
【问题讨论】:
标签: javascript c# html asp.net-mvc visual-studio