KendoUI Grid - Spalte ForeignKey nicht in der PopUp-Bearbeiten-Modus
Ich habe gesucht alle über dem Platz (understatement) für eine Lösung zu meinem Fall ohne Erfolg bis jetzt. Zunächst werde ich erklären, mein Szenario:
- Ich habe eine OpenAccess-Modell ausgesetzt, wie ein WCF Data Service (oData v3);
- Ich habe eine Kendo-MVC-Anwendung;
- Ich habe eine Ansicht mit einem raster, set für PopUp-editing, AJAX Gebunden;
Vor der Buchung einige code, lassen Sie mich erklären, mein Problem/Schwierigkeit. Ich habe eine Entität mit folgenden Eigenschaften:
- TextoID
- Titulo;
- Corpo;
- TipoTextoID;
- TipoTexto;
Gibt es eine Spalte ForeignKey gesetzt, um den TipoTextoID Eigenschaft, die ist richtig aufgefüllt, die entweder in Reihen-oder pop-up-Modus. Aber wenn es darum geht, Daten zu verändern, es funktioniert nur im Online-Modus. Das ist mein Problem ich brauche es für die Arbeit in einem popup, da die "Corpo" - Eigenschaft gebunden ist, ein KEndoUI-Editor.
Wenn das popup, es zeigt nicht den richtigen Wert auf den dropdown-weder ändert es, wenn wir wählen Sie es.
Ehrlich gesagt bin ich der dumme. Ich habe versucht fast jeder Probe, post, Artikel, die ich finden konnte ohne Erfolg, und ich bin ratlos.
Ich hoffe mir kann jemand helfen auf diese. Vielen Dank im Voraus an alle!
So, hier ist der code.
Die Aussicht:
@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>
Weiter, dann Vorlage:
@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
}
}
- Nur ein update auf diese. Für jetzt, ich habe es zu teilweise zu umgehen. In mein controller, wenn ich statt dieser: TipoTextoID = texto.TipoTextoID ich dies tun: TipoTextoID = texto.TiposTexto.TipoTextoID, den ausgewählten Wert auf die drop-down-get update. Der Nachteil ist, dass (neben der uglyness der Sache), wird die änderung nicht wider auf die Gitter...
Du musst angemeldet sein, um einen Kommentar abzugeben.
Endlich habe ich es gelöst mit der wertvollen Hilfe von KendoUI premium-Foren.
So, um dies zu verhindern, sollte man Sie nutzen den Standard-editor-Vorlage für die ForeignKeyColumn als Redakteur für die "TipoTextoID", etwa so:
Modell:
Benutzerdefinierte popup-Vorlage:
anstatt @(Html.Kendo().DropDownListFor(m => m.TipoTextoID)
Hoffe das kann anderen helfen, zu kämpfen mit der gleichen Sache.
Alle die besten!!!
Neben Stargazer ' s Antwort (vielen Dank!!!), unter benutzerdefinierte popup-template (Views\Shared\EditorTemplates\GridForeignKey.cshtml) für mich gearbeitet.
Auch, keine Notwendigkeit der Angabe der benutzerdefinierten Vorlage, die so tun, unten auf der grid-Ansicht:
Letzte Klärung, fügen Sie unten, um Haupt-Gitter-Modell (keine foreign key-viewmodel) auch dem Namen entspricht, mit custom-template.