System.Collections.Generic.IEnumerable<>does not contain a definition for 'HtmlConvertToJson and no
extension method 'HtmlConvertToJson accepting a first argument of type 'System.Wev.Mvc.HtmlHelper<System.Collections.GenericIEnumerable<> couldbe found(are you missing a using directive or an assembly
And this is the page where the error occurs at the line new ViewModel(@Html.HtmlConvertToJson(Model)));
Hope someone can help me out on this.
- @model IEnumerable<BootstrapIntroduction.Models.Author>
- @{
- ViewBag.Title = "Authors";
- }
- <h2>Authors</h2>
- <p>
- @Html.ActionLink("Create New", "Create")
- </p>
- <table class="table table-bordered table-striped">
- <thead>
- <tr>
- <th>@Html.DisplayNameFor(model => model.FirstName) </th>
- <th>@Html.DisplayNameFor(model => model.LastName) </th>
- <th>@Html.DisplayNameFor(model => model.Biography) </th>
- <th>Actions</th>
- </tr>
- </thead>
- <tbody data-bind="foreach: authors">
- <tr>
- <td data-bind="text: FirstName"></td>
- <td data-bind="text: LastName"></td>
- <td>
- <a data-bind="attr {href: '@Url.Action("Details")/' + Id }" class="btn btn-info">Details</a>
- <a data-bind="attr {href: '@Url.Action("Edit")/' + Id }" class="btn btn-primary">Edit</a>
- <a data-bind="attr: {href: '@Url.Action("Delete")/' + Id }" class="btn btn-danger">Delete</a>
- </td>
- </tr>
- </table>
- @section Scripts {
- <script>
- function ViewModel(authors) {
- var self = this;
- self.authors = authors;
- };
- var viewModel = new ViewModel(@Html.HtmlConvertToJson(Model)));
- ko.applyBindings(viewModel);
- </script>
- }
I have added in the necessary library at the HTMLHelperExtension :
- using BootstrapIntroduction.Models;
- using Newtonsoft.Json;
- using System.Web;
- using System.Web.Mvc;
- namespace BootstrapIntroduction.HtmlHelperExtension
- {
- public static class HtmlHelpherExtensions
- {
- public static HtmlString HtmlConvertToJson(this HtmlHelper htmlHelper, object model)
- {
- var settings = new JsonSerializerSettings
- {
- ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
- Formatting = Formatting.Indented
- };
- return new HtmlString(JsonConvert.SerializeObject(model, settings));
- }
- }
- }
Rajeev PunhaniPosted Jul 8, 2016, 5:55 AM
Rising SonPosted Jul 11, 2016, 12:15 PM