gpt4 book ai didi

kendo-ui - Kendo UI Grid - 外键列在弹出编辑模式下不起作用

转载 作者:行者123 更新时间:2023-12-05 08:44:02 25 4
gpt4 key购买 nike

我已经到处(轻描淡写地)搜索了我的案例的解决方案,直到现在都无济于事。首先,我将解释我的场景:

  • 我有一个公开为 WCF 数据服务 (oData v3) 的 OpenAccess 模型;
  • 我有一个 Kendo MVC 应用程序;
  • 我有一个带网格的 View ,设置为弹出式编辑,AJAX 绑定(bind);

在发布一些代码之前,让我解释一下我的问题/困难。我有一个具有这些属性的实体:

  • TextoID
  • 标题;
  • 语料库;
  • TipoTextoID;
  • TipoTexto;

有一个 ForeignKey 列设置为 TipoTextoID 属性,它可以在内嵌模式或弹出模式中正确填充。但是当涉及到更改数据时,它只适用于内联模式。这是我的问题,我需要它在弹出窗口中工作,因为“Corpo”属性绑定(bind)到 KEndoUI 编辑器。

在弹出窗口中,它不会在下拉列表中显示正确的值,当我们选择它时也不会更改它。

老实说,我觉得自己很愚蠢。我尝试了几乎所有我能找到的示例、帖子和文章,但都无济于事,而且我一无所知。

我希望有人能帮助我解决这个问题。在此先感谢大家!

所以,这是代码。观点:

    @model IEnumerable<KendoMVC.CostSimulatorService.Texto>

@{
ViewBag.Title = "Textos";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Textos</h2>

@(Html.Kendo().Grid(Model) // Bind the grid to the Model property of the view
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.Titulo); //Create a column bound to the "ProductID" property
//columns.Bound(p => p.IsPrivado).ClientTemplate("<input type='checkbox' #= IsPrivado ? checked='checked': '' # class='chkbx' />"); //Create a column bound to the "ProductName" property
columns.Template(@<text></text>).ClientTemplate("<input type='checkbox' #= IsPrivado ? checked='checked': '' # class='chkbx' />"); //Create a column bound to the "ProductName" property
//columns.Bound(p => p.TiposTexto);
columns.ForeignKey(p => p.TipoTextoID,
(System.Collections.IEnumerable)ViewData["TiposTexto"],
"TipoTextoID",
"Designacao")
.Title("Tipo de texto").Width(150);
columns.Command(command =>
{
command.Edit();
command.Destroy();
}).Width(200);
})
.ToolBar(commands => commands.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("Texto"))
.DataSource(dataSource => dataSource
.Ajax() //specify server type
.Model(model =>
{
model.Id(texto => texto.TextoID); // Specify the property which is the unique identifier of the model
model.Field(texto => texto.TextoID).Editable(false); // Make the ProductID property not editable
})
.Create(create => create.Action("CreateTexto", "BackOffice"))
.Read(read => read.Action("ReadTextos", "BackOffice"))
.Update(update => update.Action("UpdateTexto", "BackOffice"))
.Destroy(destroy => destroy.Action("DestroyTexto", "BackOffice")))
.Pageable() // Enable paging
.Sortable() // Enable sorting
.Selectable()
.Filterable()
.Scrollable()
)

<script type="text/javascript">
$(document).ready(function() {
$("form.k-edit-form").kendoValidator();
});
</script>

接下来是模板:

@using System.Web.Mvc.Html;

@model KendoMVC.CostSimulatorService.Texto

Introduza o conteúdo que deseja

@Html.HiddenFor(model => model.TextoID)
<div id="divWrapper" style="width:99%; float:left;">
<div>
@Html.LabelFor(model => model.Titulo)
</div>
<div>
@Html.EditorFor(model => model.Titulo)
@Html.ValidationMessageFor(model => model.Titulo)
</div>

<div>
@Html.LabelFor(model => model.Corpo)
</div>
<div>
@(Html.Kendo().EditorFor(model => model.Corpo))
@Html.ValidationMessageFor(model => model.Corpo)
</div>
<div>
@Html.LabelFor(model => model.TipoTextoID)
</div>
<div>
@*@(Html.Kendo().DropDownListFor(model => model.TiposTexto))
@Html.ValidationMessageFor(model => model.TiposTexto)*@
@(Html.Kendo().DropDownListFor(m => m.TipoTextoID)
.Name("TiposTexto")
.DataTextField("Designacao")
.DataValueField("TipoTextoID")
.BindTo((System.Collections.IEnumerable)
ViewData["TiposTexto"]))
</div>
</div>

Controller :

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using KendoMVC.CostSimulatorService;

namespace KendoMVC.Controllers
{
public partial class BackOfficeController : Controller
{
#region CRUD

#region ReadTextos

public ActionResult ReadTextos([DataSourceRequest]DataSourceRequest request)
{
CostSimulatorModel modelo = new CostSimulatorModel(new Uri(@"http://localhost:53212/CostSimulatorModelService.svc/"));

IQueryable<Texto> textos = modelo.Textos;
DataSourceResult resultado = textos.ToDataSourceResult(request);
ViewData["Textos"] = textos;
return Json(resultado, JsonRequestBehavior.AllowGet);
}

#endregion

#region CreateTexto

public ActionResult CreateTexto([DataSourceRequest]DataSourceRequest request, Texto texto)
{
if (ModelState.IsValid)
{
CostSimulatorModel modelo = new CostSimulatorModel(new Uri(@"http://localhost:53212/CostSimulatorModelService.svc/"));

// Create a new Product entity and set its properties from the posted ProductViewModel
Texto entity = new Texto
{
TextoID = texto.TextoID,
Titulo = texto.Titulo,
Corpo = texto.Corpo,
IsPrivado = texto.IsPrivado,
TipoTextoID = texto.TipoTextoID,
TiposTexto = texto.TiposTexto
};
modelo.AddToTextos(entity);
// Insert the entity in the database
modelo.SaveChanges();
// Get the ProductID generated by the database
texto.TextoID = entity.TextoID;
}
// Return the inserted product. The grid needs the generated ProductID. Also return any validation errors.
return Json(new[] { texto }.ToDataSourceResult(request, ModelState));
}

#endregion

#region UpdateTexto

public ActionResult UpdateTexto([DataSourceRequest]DataSourceRequest request, Texto texto)
{
if (ModelState.IsValid)
{
CostSimulatorModel modelo = new CostSimulatorModel(new Uri(@"http://localhost:53212/CostSimulatorModelService.svc/"));

// Create a new Product entity and set its properties from the posted ProductViewModel
var entity = new Texto
{
TextoID = texto.TextoID,
Titulo = texto.Titulo,
Corpo = texto.Corpo,
IsPrivado = texto.IsPrivado,
TipoTextoID = texto.TipoTextoID,
TiposTexto = texto.TiposTexto
};
// Attach the entity
modelo.AttachTo("Textos", entity);
modelo.UpdateObject(entity);
// Update the entity in the database
modelo.SaveChanges();

}
// Return the updated product. Also return any validation errors.
return Json(new[] { texto }.ToDataSourceResult(request, ModelState));
}

#endregion

#region DestroyTexto

public ActionResult DestroyTexto([DataSourceRequest]DataSourceRequest request, Texto texto)
{
if (ModelState.IsValid)
{
CostSimulatorModel modelo = new CostSimulatorModel(new Uri(@"http://localhost:53212/CostSimulatorModelService.svc/"));

// Create a new Product entity and set its properties from the posted ProductViewModel
var entity = new Texto
{
TextoID = texto.TextoID
//Titulo = texto.Titulo,
//Corpo = texto.Corpo,
//IsPrivado = texto.IsPrivado,
//TipoTextoID = texto.TipoTextoID
};
// Attach the entity
modelo.AttachTo("Textos", entity);
// Delete the entity
modelo.DeleteObject(entity);

// Delete the entity in the database
modelo.SaveChanges();
}
// Return the removed product. Also return any validation errors.
return Json(new[] { texto }.ToDataSourceResult(request, ModelState));
}

#endregion

#endregion
}
}

最佳答案

在 KendoUI 高级论坛的宝贵帮助下,我终于解决了这个问题。

因此,为了阻止这种情况发生,应该使用 ForeignKeyColumn 的默认编辑器模板作为“TipoTextoID”的编辑器,如下所示:

型号:

[UIHint("GridForeignKey")]
public int EmployeeID { get; set; }

自定义弹出模板:

@(Html.EditorFor(m => m.EmployeeID))     

而不是使用@(Html.Kendo().DropDownListFor(m => m.TipoTextoID)

希望这可以帮助其他为同样的事情而苦苦挣扎的人。

祝一切顺利!

关于kendo-ui - Kendo UI Grid - 外键列在弹出编辑模式下不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17819526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com