【问题标题】:kendo grid Add New Record is not working剑道网格添加新记录不起作用
【发布时间】:2014-02-22 09:21:14
【问题描述】:

我正在使用编辑器模板来编辑记录。但是我在绑定字段中添加外键列添加新按钮停止工作,但编辑工作正常。这是我的代码。

 @(Html.Kendo().Grid<TelerikMvcTestApp.Models.VM.ReferralViewModel>()
    .Name("grid")
    .Columns(c =>
    {
        c.Bound(i => i.ReferralDate).Title("Date");
        c.ForeignKey("AssignedMD.ID", (SelectList)ViewData["UserList"]).Title("Assigned MD").Width(200); // When I comment this line, then It works fine
c.Command(cmd =>
        {
            cmd.Edit();
            cmd.Destroy();
        });

    })
    .HtmlAttributes(new { style = "height: 500px;" })
    .Scrollable()
    .Groupable()
    .Sortable()
    .ToolBar(tb => tb.Create())
            .Editable(ed => ed.Mode(GridEditMode.PopUp).TemplateName("ReferralEdit"))
    .Pageable(pageable => pageable
        .Refresh(true)
        .PageSizes(true)
        .ButtonCount(5))
    .DataSource(dataSource =>
        dataSource
        .Ajax()
        .ServerOperation(true)
        .PageSize(10)
        .Model(model =>
        {
            model.Id(i => i.ID);
            model.Field(i => i.ID).Editable(false);
            model.Field(i => i.AssignedMD.ID).DefaultValue(1);
        })
        .Create(i => i.Action("ReferralCreate", "Referral"))
        .Read(i => i.Action("ReferralRead", "Referral"))
        .Update(i => i.Action("ReferralUpdate", "Referral").Type(HttpVerbs.Post))
        .Destroy(i => i.Action("ReferralDelete", "Referral"))

当我评论这一行时,它工作正常 c.ForeignKey("AssignedMD.ID", (SelectList)ViewData["UserList"]).Title("Assigned MD").Width(200);

【问题讨论】:

    标签: c# asp.net-mvc telerik kendo-grid


    【解决方案1】:

    您需要在网格列绑定中创建一个编辑器。不是外键列,因为该列是只读的,不会将下拉列表选择值回发到控制器中的创建函数

    ***************网格模型****************

    public Class ReferralViewModel{
    
    public DateTime UserEditorModel {get; set;}
    
    //along with other grid mode
    public UserEditorModel User {get; set;}
    
    }
    public Class UserEditorModel 
    {
      public int UserId {get; set;}
      public string UserName {get; set;}
    
    }
    

    ******************* 网格******************

    .Columns(c =>
        {
            c.Bound(i => i.ReferralDate).Title("Date");
              c.Bound(i=>i.User).EditorTemplateName("UserListEditor")
              .ClientTemplate("#=User.UserName#");
         }
    
    c.Command(cmd => {
                cmd.Edit();
                cmd.Destroy();
            });
    
        })
    

    **********编辑器************

    @(Html.Kendo().DropDownList()
        .Name("User") // Name of the widget should be the same as the name of the property    
        .DataValueField("UserId") 
        .DataTextField("UserName")    
        .BindTo(ViewBag.UserList) 
     )  
    

    ************** 创建方法 ********** em>****

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create([DataSourceRequest] DataSourceRequest request, ReferralViewModel Model)
    {
           //e.g
           var userId=Model.User.UserId;
    
     }
    

    问候 沙赫扎德

    【讨论】:

      猜你喜欢
      • 2014-06-14
      • 1970-01-01
      • 2014-08-30
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多