Kendo ui KendoUI网格-外键列在弹出编辑模式下不工作

Kendo ui KendoUI网格-外键列在弹出编辑模式下不工作,kendo-ui,kendo-grid,kendo-asp.net-mvc,Kendo Ui,Kendo Grid,Kendo Asp.net Mvc,我到处都在寻找一个轻描淡写的方法来解决我的问题,直到现在都没有用。首先,我将解释我的场景: 我有一个OpenAccess模型作为WCF数据服务ODatav3公开; 我有一个剑道MVC应用程序; 我有一个网格视图,设置为弹出式编辑,AJAX绑定; 在发布一些代码之前,让我解释一下我的问题/困难。我有一个具有以下属性的实体: 文本类 提图洛; 科波; 三叶草; 提波泰索; TIPOTEXOID属性设置了一个ForeignKey列,get以串联或弹出模式正确填充该列。但是,当涉及到更改数据时,它只在串

我到处都在寻找一个轻描淡写的方法来解决我的问题,直到现在都没有用。首先,我将解释我的场景:

我有一个OpenAccess模型作为WCF数据服务ODatav3公开; 我有一个剑道MVC应用程序; 我有一个网格视图,设置为弹出式编辑,AJAX绑定; 在发布一些代码之前,让我解释一下我的问题/困难。我有一个具有以下属性的实体:

文本类 提图洛; 科波; 三叶草; 提波泰索; TIPOTEXOID属性设置了一个ForeignKey列,get以串联或弹出模式正确填充该列。但是,当涉及到更改数据时,它只在串联模式下工作。这是我的问题,我需要它在弹出窗口中工作,因为Corpo属性绑定到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>
@(Html.EditorFor(m => m.EmployeeID))     
控制员:

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; }
自定义弹出窗口模板:

@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>
@(Html.EditorFor(m => m.EmployeeID))     
而不是使用@Html.Kendo.DropDownListForm=>m.TipoTextoID

希望这能帮助其他人解决同样的问题


祝你一切顺利

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

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

型号:

[UIHint("GridForeignKey")]
public int EmployeeID { get; set; }
自定义弹出窗口模板:

@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>
@(Html.EditorFor(m => m.EmployeeID))     
而不是使用@Html.Kendo.DropDownListForm=>m.TipoTextoID

希望这能帮助其他人解决同样的问题


祝你一切顺利

除了观星者的回答,谢谢!!!,下面的自定义弹出模板视图\Shared\EditorTemplates\GridForeignKey.cshtml对我很有用

@model object

@(
 Html.Kendo().DropDownListFor(m => m)
        .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
)
此外,无需指定自定义模板,请在网格视图上执行以下操作:

.Editable(editable => editable.Mode(Kendo.Mvc.UI.GridEditMode.PopUp))
最后一点说明,将以下内容添加到主网格模型中,而不是外键viewmodel名称也与自定义模板匹配

[UIHint("GridForeignKey")]

除了观星者的回答,谢谢!!!,下面的自定义弹出模板视图\Shared\EditorTemplates\GridForeignKey.cshtml对我很有用

@model object

@(
 Html.Kendo().DropDownListFor(m => m)
        .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
)
此外,无需指定自定义模板,请在网格视图上执行以下操作:

.Editable(editable => editable.Mode(Kendo.Mvc.UI.GridEditMode.PopUp))
最后一点说明,将以下内容添加到主网格模型中,而不是外键viewmodel名称也与自定义模板匹配

[UIHint("GridForeignKey")]

只是更新一下。现在,我让它部分工作。在我的控制器中,如果我执行以下操作:TipoTextoID=texto.TipoTextoID,而不是:TipoTextoID=texto.TiposTexto.TipoTextoID,则下拉菜单上的选定值会更新。不利的一面是,除了丑陋的东西,变化并没有反映在网格上…只是更新了这个。现在,我让它部分工作。在我的控制器中,如果我执行以下操作:TipoTextoID=texto.TipoTextoID,而不是:TipoTextoID=texto.TiposTexto.TipoTextoID,则下拉菜单上的选定值会更新。缺点是,除了事情的丑陋之外,变化并没有反映在网格上……谢谢你的回答。我不会问你是怎么知道的我在试着猜测我应该做些什么来让它也起作用。然而,我需要在弹出窗口中使用DropDownList,因为它是带有服务器绑定和其他参数的级联下拉列表。为了得到正确的答案,[UIHintGridForeignKey]实际上做了什么,它是如何成为解决方案的一部分的?使用该属性,您告诉MVC如何继续。渲染时,它将使用应出现在Views/Shared/EditorTemplates中的GridForeignKey编辑器模板。另见@Manish Jain answear关于主题的内容谢谢你的回答。我不会问你是怎么知道的我在试着猜测我应该做些什么来让它也起作用。然而,我需要在弹出窗口中使用DropDownList,因为它是带有服务器绑定和其他参数的级联下拉列表。为了得到正确的答案,[UIHintGridForeignKey]实际上做了什么,它是如何成为解决方案的一部分的?使用该属性,您告诉MVC如何继续。渲染时,它将使用应出现在Views/Shared/EditorTemplates中的GridForeignKey编辑器模板。关于该主题,另见@Manish Jain answear