在 kendo 网格的编辑器模板中获取 属性 的值
Get value of property in editor template of kendo grid
我有 kendo 网格和 ajax 绑定
@(Html.Kendo().Grid<LightViewModelExtended>()
.Name("LightsGrid")
.Columns(col =>
{
col.Bound(x => x.LightID)
.ClientTemplate(Html.ActionLink("#: LightID #", "Edit", new { id = "#: LightID #" }).ToHtmlString());
col.Bound(x => x.Name);
col.Command(command => command.Edit());
col.Command(command => command.Destroy());
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("LightExtended"))
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => model.Id(x => x.LightID))
.Read(read => read.Action("GetLights", "Lights"))
.Create(create => create.Action("CreateLight", "Lights"))
.Update(update => update.Action("UpdateLight", "Lights"))
.Destroy(destroy => destroy.Action("DeleteLight", "Lights"))
))
我也有一个编辑器模板
<table>
<tr>
<td>
@Html.LabelFor(model => model.Name)
</td>
<td>
@Html.KendoTextBoxFor(model => model.Name)
</td>
<td>
@Html.ValidationMessageFor(model => model.Name)
</td>
</tr>
</table>
我想在我的编辑器模板中显示操作 link
@Html.ActionLink(Model.LightID + ".ies", "Download", "Lights", new { id = Model.LightID }, null)
所以它看起来像
<table>
<tr>
<td>
@Html.LabelFor(model => model.Name)
</td>
<td>
@Html.KendoTextBoxFor(model => model.Name)
</td>
<td>
@Html.ValidationMessageFor(model => model.Name)
</td>
<td>
@Html.ActionLink(Model.LightID + ".ies", "Download", "Lights", new { id = Model.LightID }, null)
</td>
</tr>
</table>
然后给我 links,比如“1.ies”、“2.ies”等等,这会引导我到 http://mysite/Lights/1,其中 1 是 ID,但它看起来像“0.ies”
我知道在使用 ajax 绑定时模型 属性 没有填充数据,但我找不到实现此目的的正确方法
我已经尝试了 @Html.ValueFor
和 #: LightID #
,但都没有用
请尝试使用以下代码片段。如果我不明白您的要求,请告诉我。
VIEW
@(Html.Kendo().Grid<MvcApplication1.Models.TestModel>()
.Name("LightsGrid")
.Columns(col =>
{
col.Bound(x => x.ID)
.ClientTemplate(Html.ActionLink("#: ID #", "Edit", new { id = "#: ID #" }).ToHtmlString());
col.Bound(x => x.Name);
col.Command(command => command.Edit());
col.Command(command => command.Destroy());
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("Partial1"))
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => model.Id(x => x.ID))
.Read(read => read.Action("GetData", "Home"))
.Create(create => create.Action("CreateData", "Home"))
.Update(update => update.Action("UpdateData", "Home"))
.Destroy(destroy => destroy.Action("DestroyData", "Home"))
)
)
局部视图 ("Views\Shared\EditorTemplates\Partial1.cshtml")
@model MvcApplication1.Models.TestModel
<table>
<tr>
<td>
@Html.LabelFor(model => model.Name)
</td>
<td>
@Html.Kendo().TextBoxFor(model => model.Name)
</td>
<td>
@Html.ValidationMessageFor(model => model.Name)
</td>
<td>
@Html.HiddenFor(model => model.ID)
@Html.ActionLink(Model.ID + ".iso", "Download", "Lights", null, new { id = "Download" })
</td>
</tr>
</table>
<script>
var myVar = setInterval(function () { myTimer() }, 300);
$(document).ready(function () {
});
function myTimer() {
if ($("#ID") != undefined && $("#ID") != null) {
if ($("#ID").val() == "0") {
}
else {
$("#Download").html($("#ID").val() + ".iso");
$("#Download").prop("href", $("#Download").prop("href") + "/" + $("#ID").val());
clearInterval(myVar);
}
}
}
</script>
控制器
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult GetData([DataSourceRequest] DataSourceRequest request)
{
List<TestModel> lst = new List<TestModel>();
lst.Add(new TestModel() { ID = 1, Name = "Name1" });
lst.Add(new TestModel() { ID = 2, Name = "Name2" });
lst.Add(new TestModel() { ID = 3, Name = "Name3" });
return Json(lst.ToDataSourceResult(request));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateData([DataSourceRequest] DataSourceRequest request, TestModel model)
{
if (model != null && ModelState.IsValid)
{
//productService.Create(product);
}
return Json(new[] { model }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateData([DataSourceRequest] DataSourceRequest request, TestModel model)
{
if (model != null && ModelState.IsValid)
{
//productService.Update(product);
}
return Json(new[] { model }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DestroyData([DataSourceRequest] DataSourceRequest request, TestModel model)
{
if (model != null)
{
//productService.Destroy(product);
}
return Json(new[] { model }.ToDataSourceResult(request, ModelState));
}
}
}
型号
namespace MvcApplication1.Models
{
public class TestModel
{
public int ID { get; set; }
public string Name { get; set; }
}
}
@Html.ValueFor
已在 Kendo 2014.3.1411
中修复
我有 kendo 网格和 ajax 绑定
@(Html.Kendo().Grid<LightViewModelExtended>()
.Name("LightsGrid")
.Columns(col =>
{
col.Bound(x => x.LightID)
.ClientTemplate(Html.ActionLink("#: LightID #", "Edit", new { id = "#: LightID #" }).ToHtmlString());
col.Bound(x => x.Name);
col.Command(command => command.Edit());
col.Command(command => command.Destroy());
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("LightExtended"))
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => model.Id(x => x.LightID))
.Read(read => read.Action("GetLights", "Lights"))
.Create(create => create.Action("CreateLight", "Lights"))
.Update(update => update.Action("UpdateLight", "Lights"))
.Destroy(destroy => destroy.Action("DeleteLight", "Lights"))
))
我也有一个编辑器模板
<table>
<tr>
<td>
@Html.LabelFor(model => model.Name)
</td>
<td>
@Html.KendoTextBoxFor(model => model.Name)
</td>
<td>
@Html.ValidationMessageFor(model => model.Name)
</td>
</tr>
</table>
我想在我的编辑器模板中显示操作 link
@Html.ActionLink(Model.LightID + ".ies", "Download", "Lights", new { id = Model.LightID }, null)
所以它看起来像
<table>
<tr>
<td>
@Html.LabelFor(model => model.Name)
</td>
<td>
@Html.KendoTextBoxFor(model => model.Name)
</td>
<td>
@Html.ValidationMessageFor(model => model.Name)
</td>
<td>
@Html.ActionLink(Model.LightID + ".ies", "Download", "Lights", new { id = Model.LightID }, null)
</td>
</tr>
</table>
然后给我 links,比如“1.ies”、“2.ies”等等,这会引导我到 http://mysite/Lights/1,其中 1 是 ID,但它看起来像“0.ies”
我知道在使用 ajax 绑定时模型 属性 没有填充数据,但我找不到实现此目的的正确方法
我已经尝试了 @Html.ValueFor
和 #: LightID #
,但都没有用
请尝试使用以下代码片段。如果我不明白您的要求,请告诉我。
VIEW
@(Html.Kendo().Grid<MvcApplication1.Models.TestModel>()
.Name("LightsGrid")
.Columns(col =>
{
col.Bound(x => x.ID)
.ClientTemplate(Html.ActionLink("#: ID #", "Edit", new { id = "#: ID #" }).ToHtmlString());
col.Bound(x => x.Name);
col.Command(command => command.Edit());
col.Command(command => command.Destroy());
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("Partial1"))
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => model.Id(x => x.ID))
.Read(read => read.Action("GetData", "Home"))
.Create(create => create.Action("CreateData", "Home"))
.Update(update => update.Action("UpdateData", "Home"))
.Destroy(destroy => destroy.Action("DestroyData", "Home"))
)
)
局部视图 ("Views\Shared\EditorTemplates\Partial1.cshtml")
@model MvcApplication1.Models.TestModel
<table>
<tr>
<td>
@Html.LabelFor(model => model.Name)
</td>
<td>
@Html.Kendo().TextBoxFor(model => model.Name)
</td>
<td>
@Html.ValidationMessageFor(model => model.Name)
</td>
<td>
@Html.HiddenFor(model => model.ID)
@Html.ActionLink(Model.ID + ".iso", "Download", "Lights", null, new { id = "Download" })
</td>
</tr>
</table>
<script>
var myVar = setInterval(function () { myTimer() }, 300);
$(document).ready(function () {
});
function myTimer() {
if ($("#ID") != undefined && $("#ID") != null) {
if ($("#ID").val() == "0") {
}
else {
$("#Download").html($("#ID").val() + ".iso");
$("#Download").prop("href", $("#Download").prop("href") + "/" + $("#ID").val());
clearInterval(myVar);
}
}
}
</script>
控制器
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult GetData([DataSourceRequest] DataSourceRequest request)
{
List<TestModel> lst = new List<TestModel>();
lst.Add(new TestModel() { ID = 1, Name = "Name1" });
lst.Add(new TestModel() { ID = 2, Name = "Name2" });
lst.Add(new TestModel() { ID = 3, Name = "Name3" });
return Json(lst.ToDataSourceResult(request));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateData([DataSourceRequest] DataSourceRequest request, TestModel model)
{
if (model != null && ModelState.IsValid)
{
//productService.Create(product);
}
return Json(new[] { model }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateData([DataSourceRequest] DataSourceRequest request, TestModel model)
{
if (model != null && ModelState.IsValid)
{
//productService.Update(product);
}
return Json(new[] { model }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DestroyData([DataSourceRequest] DataSourceRequest request, TestModel model)
{
if (model != null)
{
//productService.Destroy(product);
}
return Json(new[] { model }.ToDataSourceResult(request, ModelState));
}
}
}
型号
namespace MvcApplication1.Models
{
public class TestModel
{
public int ID { get; set; }
public string Name { get; set; }
}
}
@Html.ValueFor
已在 Kendo 2014.3.1411