最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Asp net core - from model to javascript - Stack Overflow

programmeradmin4浏览0评论

I'm writing an asp net core application. What I want to achieve is to read the model inside the view with Javascript. I found this code but when I run it i receive this error:

'IJsonHelper' does not contain a definition for 'Encode' and no extension method 'Encode' accepting a first argument of type 'IJsonHelper' could be found (are you missing a using directive or an assembly reference?)

how can i fix it?

controller

public async Task<IActionResult> Index()
{
    return View(await _context.Bolla.ToListAsync());
}

view

@model IEnumerable<ps0001.Models.Bolla>

<script>
    var bolla = @Html.Raw(Json.Encode(Model));
</script>

I'm writing an asp net core application. What I want to achieve is to read the model inside the view with Javascript. I found this code but when I run it i receive this error:

'IJsonHelper' does not contain a definition for 'Encode' and no extension method 'Encode' accepting a first argument of type 'IJsonHelper' could be found (are you missing a using directive or an assembly reference?)

how can i fix it?

controller

public async Task<IActionResult> Index()
{
    return View(await _context.Bolla.ToListAsync());
}

view

@model IEnumerable<ps0001.Models.Bolla>

<script>
    var bolla = @Html.Raw(Json.Encode(Model));
</script>
Share Improve this question edited Apr 25, 2017 at 8:02 user3559349 asked Apr 24, 2017 at 16:20 carlezcarlez 931 gold badge1 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 22

Try using this in your view instead:

@model IEnumerable<ps0001.Models.Bolla>

<script>
    var bolla = '@Html.Raw(Json.Serialize(Model))';
</script>

EDIT:

In order to view the contents, parse the extracted Model using the following:

var parseModel = JSON.parse(bolla);

Then you will be able to use the object and whatever attributes it contains.

发布评论

评论列表(0)

  1. 暂无评论