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

时间:2022-06-02 20:33:13

I have kendo grid with ajax binding

我有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"))
  ))

I also have an editor template

我还有一个编辑器模板

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

I want to show action link in my editor template

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

@Html.ActionLink(Model.LightID + ".ies", "Download", "Lights", new { id = Model.LightID }, null)

so it would look like

看起来是这样的

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

And give me links like "1.ies", "2.ies" and so on which leads me to http://mysite/Lights/1 where 1 is ID, but it looks like "0.ies"

给我一些链接,比如“1”理论”、“2。然后指向http://sitemy/lights/1其中1是ID,但它看起来像0

I know that Model property is not populated with data while using ajax binding, but I can't find a proper way to achieve this

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

I already tried @Html.ValueFor and #: LightID # but it didn't work either

我已经试过@Html。ValueFor和#:LightID #但它也不起作用

2 个解决方案

#1


1  

Please try with the below code snippet. Let me know if I am not understand your requirement.

请尝试使用下面的代码片段。如果我不明白你的要求,请告诉我。

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

PARTIAL VIEW ("Views\Shared\EditorTemplates\Partial1.cshtml")

局部视图(视图\ \ 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; }
    }
}

#2


1  

@Html.ValueFor was fixed in Kendo 2014.3.1411

@Html。ValueFor固定在Kendo 2014.3.1411

#1


1  

Please try with the below code snippet. Let me know if I am not understand your requirement.

请尝试使用下面的代码片段。如果我不明白你的要求,请告诉我。

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

PARTIAL VIEW ("Views\Shared\EditorTemplates\Partial1.cshtml")

局部视图(视图\ \ 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; }
    }
}

#2


1  

@Html.ValueFor was fixed in Kendo 2014.3.1411

@Html。ValueFor固定在Kendo 2014.3.1411