将新行保存到我的kendo网格时遇到问题。 当id被发送到服务器时它是0.但是当我尝试在服务器上更改它时它仍然是0.
我该如何解决这个问题?我很乐意帮助解决这个问题。
我的Index.cshtml
@{
ViewBag.Title = "Book List";
}
@{
var defaultAuthor = ViewData["defaultAuthor"]
as List<Number2.Models.AuthorViewModel>;
var temp = defaultAuthor.FirstOrDefault().AuthorName as string;
}
<script type="text/kendo" id="authorsTemplate">
<ul>
#if(data.Authors == null)
{ #
data.Authors = @Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewData["defaultAuthor"]))
# } #
#if(data.Authors != null)
{ #
# 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"]))
});
}
//added var grid
var grid = $("#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",
//complete: function (e) {
// $("#grid").data("kendoGrid").dataSource.read();
//},
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: [
{ field: "BookName", width: 90, title: "Book Name" },
"Pages",
// { editor: GenreEditor, field: "Genre" },IEnumerable<Number2.Models.AuthorViewModel>
"Genre",
"Publisher",
{
editor: authorsEditor,
field: "Authors",
template: $("#authorsTemplate").html(),
@*defaultValue: @Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewData["defaultAuthor"]))*@
},
{ command: ["edit", "destroy"] }
]
});
//$(".k-grid-my-create", grid.element).on("click", function (e) {
// var dataSource = grid.dataSource;
// var total = dataSource.data();
// dataSource.insert(total, {});
// dataSource.page(dataSource.totalPages());
// grid.editRow(grid.tbody.children().last());
//});
</script>
GridController
我尝试更改create方法中的值,但它无效。
[HttpPost]
public ActionResult Update(BookViewModel book)
{
if (ModelState.IsValid)
{
repository.UpdateBook(book);
}
return Json(new[] { book }.ToDataSourceResult(new DataSourceRequest(), ModelState));
}
[HttpPost]
public ActionResult Create(BookViewModel book)
{
if (ModelState.IsValid)
{
book.BookId = repository.CreateBook(book);
}
return Json(new[] { book }.ToDataSourceResult(new DataSourceRequest(), ModelState), JsonRequestBehavior.AllowGet);
}
预订模型
namespace Number2.Models
{
public class Book
{
public Book()
{
}
[ScaffoldColumn(false)]
public int BookId { get; set; }
public string BookName { get; set; }
public int Pages { get; set; }
public string Publisher { get; set; }
public Genre Genre { get; set; }
[JsonIgnore]
public virtual ICollection<Author> Authors { get; set; }
}
}
作者模型
namespace Number2.Models
{
public class Author
{
public Author()
{
}
[Required]
public int AuthorId { get; set; }
[Display(Name = "Author Name")]
[MaxLength(100, ErrorMessage = "Author Name must be 100 characters or less"), MinLength(5)]
public string AuthorName { get; set; }
[JsonIgnore]
public virtual ICollection<Book> Books { get; set; }
}
}
BookViewModel
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;
}
}
}