在kendo网格的编辑器模板中获取属性值

时间:2015-01-19 18:35:33

标签: asp.net-mvc kendo-ui kendo-grid

我有带有ajax绑定的kendo网格

@(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>

我想在我的编辑器模板中显示操作链接

@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>

并给我链接,如“1.ies”,“2.ies”等等,这导致我http://mysite/Lights/1,其中1是ID,但它看起来像“0.ies”

我知道在使用ajax绑定时Model属性没有填充数据,但我找不到合适的方法来实现这个

我已经尝试了@Html.ValueFor#: LightID #,但它无法正常使用

2 个答案:

答案 0 :(得分:1)

请尝试使用以下代码段。如果我不理解您的要求,请告诉我。

查看

@(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"))
  )
)

PARTIAL VIEW (“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>

<强> CONTROLLER

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));
        }
    }
}

<强> MODEL

namespace MvcApplication1.Models
{
    public class TestModel
    {
        public int ID { get; set; }

        public string Name { get; set; }
    }
}

答案 1 :(得分:1)

@Html.ValueFor已在Kendo 2014.3.1411

中修复