4
我對Telerik產品很熟悉,但對Kendo UI MVC包裝器很陌生。我最初幾個月下載了它們,並且從未實際嘗試過,因此我的試用支持已經過期。kendo ui grid - 新手無法獲得基本示例工作
我在VS.NET中創建了一個全新的Kendo UI MVC應用程序。
我創建了我自己的控制器和視圖。
我複製了批處理編輯示例(http://demos.kendoui.com/web/grid/editing.html),並換出了示例所用數據的靜態列表。
網格正確顯示我的數據。
但是,單擊保存時,更新不會向服務器({})發送任何內容。單擊保存時,刪除也不會向服務器({})發送任何內容。創建每個新項目發送記錄,但Name屬性設置爲null。
任何想法?
我的代碼如下。
感謝, 凱文
*我的模型*
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace KendoUIMvcApplication1.Models
{
public class PersonalInterestModel
{
public int ID;
public string Name;
}
}
*我的控制器*
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using KendoUIMvcApplication1.Models;
namespace KendoUIMvcApplication1.Controllers
{
public class ManagerController : Controller
{
//
// GET: /Manager/
private static List<PersonalInterestModel> items = new List<PersonalInterestModel>();
static ManagerController()
{
items.Add(new PersonalInterestModel() { ID = 1, Name = "Finance" });
items.Add(new PersonalInterestModel() { ID = 2, Name = "Construction" });
items.Add(new PersonalInterestModel() { ID = 3, Name = "Technology" });
items.Add(new PersonalInterestModel() { ID = 4, Name = "Entertainment" });
}
public ActionResult Index()
{
return View();
}
public ActionResult Editing_Read([DataSourceRequest] DataSourceRequest request)
{
return Json(items.ToDataSourceResult(request));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Editing_Create([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<PersonalInterestModel> products)
{
var results = new List<PersonalInterestModel>();
if (products != null && ModelState.IsValid)
{
foreach (var product in products)
{
product.ID = items.Max(i => i.ID) + 1;
items.Add(product);
results.Add(product);
}
}
return Json(results.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Editing_Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<PersonalInterestModel> products)
{
if (products != null && ModelState.IsValid)
{
foreach (var product in products)
{
var target = items.Find(p => p.ID == product.ID);
if (target != null)
{
target.Name = product.Name;
}
}
}
return Json(ModelState.ToDataSourceResult());
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Editing_Destroy([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<PersonalInterestModel> products)
{
if (products.Any())
{
foreach (var product in products)
{
items.Remove(items.Find(p => p.ID == product.ID));
}
}
return Json(ModelState.ToDataSourceResult());
}
}
}
*我查看*
@{
ViewBag.Title = "Personal Interests";
}
<h2>Index</h2>
@(Html.Kendo().Grid<KendoUIMvcApplication1.Models.PersonalInterestModel>()
.Name("Grid")
.Columns(columns => {
columns.Bound(p => p.ID).Width(50);
columns.Bound(p => p.Name).Width(140);
columns.Command(command => command.Destroy()).Width(110);
})
.ToolBar(toolbar => {
toolbar.Create();
toolbar.Save();
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Sortable()
.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()
.Batch(true)
.ServerOperation(false)
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.ID))
.Create("Editing_Create", "Manager")
.Read("Editing_Read", "Manager")
.Update("Editing_Update", "Manager")
.Destroy("Editing_Destroy", "Manager")
)
)
<script type="text/javascript">
function error_handler(e) {
if (e.errors) {
var message = "Errors:\n";
$.each(e.errors, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function() {
message += this + "\n";
});
}
});
alert(message);
}
}
</script>
試過了,它似乎解決了創建(名稱值不再通過爲空,但它不會幫助更新和刪除。更新和刪除仍將空的JSON對象發送到服務器。 – retsvek
所以當我最初嘗試時,它似乎沒有完全工作。但今天早上我一定很累,沒有改變任何東西,它的運作很完美。謝謝!而該死的制定者/獲得者! – retsvek
順便說一句 - 我不能將它標記爲答案,因爲我的代表是全新的... – retsvek