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
1 Answer
Reset to default 22Try 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.