剑道网格'属性'未定义

时间:2018-02-27 14:01:54

标签: javascript c# entity-framework-6 kendo-grid kendo-multiselect

抱歉我的英文。 当我尝试向网格添加新行时,我有“SCRIPT5009:'作者'未定义”。我试图从我的ViewBag设置属性的默认值,但它不起作用。可能是某人已经遇到此错误。 任何建议都会很高兴。 开发人员工具说,作者没有在这里定义(在我的authorsTemplate中)

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

但我怎样才能正确设置默认值? “columns”中的默认值属性没有看到我的ViewBag

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

GridController (在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>

视图模型:

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;
    }
}

} 使用解决方案进行更新:

1.编辑我的编辑

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

2。调试后,我发现在创建新记录后网格没有设置我的id。

搜索继续......

0 个答案:

没有答案