1

Sorry for my English. I've got "SCRIPT5009: 'Authors' is not defined" when I tried to add a new row to the grid. I tried to set a default value for property from my ViewBag but it doesn't work. May be someone already faced this error. Will be glad to any advice. The developer tools say that Authors not defined here(in my authorsTemplate)

for (var i = 0; i < Authors.length; i++) {
        ;$kendoOutput += '\n        <li>' + 
$kendoHtmlEncode(Authors[i].AuthorName) + '</li>\n

But how can i set default value correctly? default value property in "columns" doesnt see my ViewBag

 {
            editor: authorsEditor, field: "Authors", template: $("#authorsTemplate").html(),
            defaultValue: "@ViewData["defaultAuthor"]"
        },

GridController(typical operations to save data in db)

public ActionResult Create(BookViewModel book)
    {

        if (ModelState.IsValid)
        {
            book.BookId = repository.CreateBook(book);
            //repository.CreateBook(book);
        }

        return Json(new[] { book }.ToDataSourceResult(new DataSourceRequest(), ModelState), JsonRequestBehavior.AllowGet);
    }

Index.cshtml:

@{
    ViewBag.Title = "Book List";
}
<script type="text/kendo" id="authorsTemplate">
    <ul>
        # for(var i = 0; i < Authors.length; i++){ #
        <li>#: Authors[i].AuthorName #</li>
        # } #
    </ul>
</script>

<div id="grid"></div>
<script>
    function authorsEditor(container, options) {
        $('<input name="Authors">').appendTo(container)
            .kendoMultiSelect({
                dataValueField: "AuthorId",
                dataTextField: "AuthorName",
                dataSource: @Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewData["authors"]))
            });
    }

    $("#grid").kendoGrid({
        pageable: true,
        toolbar: ["create"],
        dataSource: {
            pageSize: 3,
            transport: {
                read: {
                    url: "@Url.Action("Read", "Grid")",
                    type: "POST"
                },
                update: {
                    url: "@Url.Action("Update", "Grid")",
                    contentType: "application/json",
                    type: "POST"
                },
                create: {
                    url: "@Url.Action("Create", "Grid")",
                    contentType: "application/json",//hz nado li
                    type: "POST"
                },
                 destroy: {
                    url: "@Url.Action("Destroy", "Grid")",
                    contentType: "application/json",//hz nado li
                    type: "POST"
                },
                parameterMap: function(options) {
                    return kendo.stringify(options);
                }
            },
            schema: {
                model: {
                    id: "BookId",
                    fields: {
                        AuthorName: { type: "string" }
                    }
                }
            }
        },
        editable: "inline",
        columns: [
            "BookName",
            "Pages",
        //    { editor: GenreEditor, field: "Genre" },IEnumerable<Number2.Models.AuthorViewModel>
            "Genre",
            "Publisher",
            {
                editor: authorsEditor, field: "Authors", template: $("#authorsTemplate").html()

            },
            { command: ["edit", "destroy"] }
        ]
    });
</script>

ViewModel:

namespace Number2.Models
{
public class BookViewModel
{
    public BookViewModel()
    {
        Authors = new List<AuthorViewModel>();
    }
    [ScaffoldColumn(false)]
    public int BookId { get; set; }

    [Display(Name = "Book Name")]
    [MaxLength(100, ErrorMessage = "Book Name must be 100 characters or less"), MinLength(5)]
    public string BookName { get; set; }
    public int Pages { get; set; }

    public string Publisher { get; set; }

    [Range(0, maximum: 10)]
    public Genre Genre { get; set; }

    [UIHint("AuthorsEditor")]
    public virtual List<AuthorViewModel> Authors { get; set; }
    public void CopyToBook(Book book)
    {
        book.BookId = BookId;
        book.BookName = BookName;
        book.Pages = Pages;
        book.Genre = Genre;
        book.Publisher = Publisher;
    }
}

} Update with Solution:

1.Edited my editor

 #if(data.Authors != null)
    { #
    # for(var i = 0; i < Authors.length; i++){ #
    <li>#: Authors[i].AuthorName #</li>
    # } #
    # } #

2. After debugging I discovered that grid doesn't set my id after creating a new record.

the search continues...

4
  • The developer tools in any browser will help, but one hint is you should make sure that how you use "Authors" is case sensitive, either as a String or object.
    – user1531971
    Feb 27, 2018 at 14:57
  • The developer tools say that Authors not defined here: for (var i = 0; i < Authors.length; i++) { ;$kendoOutput += '\n <li>' + $kendoHtmlEncode(Authors[i].AuthorName) + '</li>\n But how can i set default value correctly?
    – Vlad
    Feb 28, 2018 at 8:22
  • I'd look everywhere in your code for uses of "Authors" and "authors", either as a string or an object. Ask yourself why Authors.length seems to be dereferencing a null reference. That is, what sort of thing is Authors and how are you expecting it to not only be non-null, but possible non-zero as well. BTW, you should always edit your question and add details when providing clarification based on comments.
    – user1531971
    Feb 28, 2018 at 14:54
  • Thank you for your answer, I have fixed this issue(by adding check on null, because my authors column hasn't default value) but there was a new one with the BookId. You can see this question here Cannot save a new row in kendo grid
    – Vlad
    Mar 1, 2018 at 9:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy