嗨,任何人都可以帮助我,我正在努力解决我的代码问题。我正在尝试两天如何在网格中添加下拉列表我正在尝试修复我的代码,但我无法找到为什么丢弃框没有在我的网格中显示我的代码是
<%: Html.Kendo().Grid<KendoGridAjaxEditing2.Models.ProductViewModel>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(product => product.CustomerID).Width(100);
columns.Bound(product => product.CustomerName).ClientTemplate("#=Category.CustomerFName#").Width(160);
columns.Bound(product => product.CustomerLastName);
columns.Bound(product => product.Customerage).Width(250);
columns.Command(commands =>
{
commands.Edit(); // The "edit" command will edit and update data items
commands.Destroy(); // The "destroy" command removes data items
}).Title("Commands").Width(200);
})
.ToolBar(toolbar => toolbar.Create()) // The "create" command adds new data items
.Editable(editable => editable.Mode(GridEditMode.InLine)) // Use inline editing mode
.DataSource(dataSource =>
dataSource.Ajax()
.Model(model =>
{
model.Id(product => product.CustomerID); // Specify the property which is the unique identifier of the model
model.Field(product => product.CustomerID).Editable(false); // Make the ProductID property not editable
model.Field(p => p.Category).DefaultValue(
ViewData["defaultCategory"] as KendoGridAjaxEditing2.Models.ClientCategoryViewModel);
})
.Create(create => create.Action("Products_Create", "Home")) // Action invoked when the user saves a new data item
.Read(read => read.Action("Products_Read", "Home")) // Action invoked when the grid needs data
.Update(update => update.Action("Products_Update", "Home")) // Action invoked when the user saves an updated data item
.Destroy(destroy => destroy.Action("Products_Destroy", "Home")) // Action invoked when the user removes a data item
)
.Pageable()
%>
和HomeController方法
private void PopulateCategories()
{
var dataContext = new NorthwindEntities();
var categories = dataContext.Customer_details
.Select(c => new ClientCategoryViewModel
{
ID = c.ID,
CustomerFName = c.name
})
.OrderBy(e => e.CustomerFName);
ViewData["categories"] = categories;
ViewData["defaultCategory"] = categories.First();
}
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
PopulateCategories();
return View();
}
和我的ClientCategoryViewModel类
public class ClientCategoryViewModel
{
public int ID { get; set; }
public string CustomerFName { get; set; }
}
当我尝试.ClientTemplate(“#= Category.CustomerFName#”)。宽度(160);在我看来我没有结果,但一旦我删除它我的网格显示记录,但我无法理解为什么我没有得到我的网格中的下拉,我正在尝试这最后2至3天,我无法找出一个解决方案,我试图将此链接作为指南http://demos.kendoui.com/web/grid/editing.html,但问题仍然存在请有人帮助我谢谢
答案 0 :(得分:0)
ClientTemplate用于显示模式,对于编辑模式,您需要创建DropDownList编辑器。检查more information的以下主题。如果你按照那里的指示,你应该让它工作。
答案 1 :(得分:0)
您需要使用columns.ForeignKey,如本演示中所示:http://demos.kendoui.com/web/grid/foreignkeycolumn.html
确保您拥有GridForeignKey.cshtml编辑器模板。(通常位于Views \ Shared \ EditorTemplates中)
您很可能希望将其绑定到CustomerId而不是名称。 我通常绑定到IEnumerable。出于某种原因,Kendo告诉它“Value”是值列,“Text”是文本列,即使它们不能让你为正常的非网格下拉做到这一点。
如果您需要将其空白,请将其绑定到以下行:
private IEnumerable<SelectListItem> PaymentTermsList
{
get
{
if (paymentTermsList == null)
{
paymentTermsList =
new [] { new SelectListItem(){ Value = "-1", Text = "(None)" } }.Concat(
referenceDataService.GetActivePaymentTerms()
.Select(pt => new SelectListItem() { Value = pt.PaymentTermId.ToString(), Text = pt.Name }));
}
return paymentTermsList;
}
}