kendo grid添加新记录不起作用

时间:2014-02-22 09:21:15

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

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

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

1 个答案:

答案 0 :(得分:0)

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

** * ** * ** * 的** * ** * 网格模型 ** * < / EM> ** * ** * ** * ** * *

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

** * ** * ** * 的** * ** 创建方法 * ** * ** * * * * ****

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

 }

问候 沙赫扎德