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...
Authors.length
seems to be dereferencing a null reference. That is, what sort of thing isAuthors
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.