Telerik MVC插入命令不触发控制器

时间:2012-05-17 21:35:20

标签: asp.net-mvc gridview insert telerik

我希望有人能够帮助我弄清楚出了什么问题。我无法获取我的插入命令来触发控制器,无法弄清楚为什么它不会。这是我的代码:

    @(Html.Telerik().Splitter().Name("AnswerOptionSplitter")
        .Orientation(SplitterOrientation.Horizontal)
        .Panes(panes =>
        {
            panes.Add()
                .Size("28px")
                .Resizable(false)
                .Content(@<text>
                <div class="entity-tools-vertical">
                    <button id="editAnswerOption" title="Edit" class="entity-action"><img alt="Edit" height="24" src="@Url.Content("~/Content/Common/Images/Icons/edit-disabled.png")" width="24" /></button>
                    <button id="moveUpAnswerOption" title="Move Up" class="entity-action"><img alt="Move Up" height="24" src="@Url.Content("~/Content/Common/Images/Icons/up-disabled.png")" width="24" /></button>
                    <button id="moveDownAnswerOption" title="Move Down" class="entity-action"><img alt="Move Down" height="24" src="@Url.Content("~/Content/Common/Images/Icons/down-disabled.png")" width="24" /></button>
                    <button id="toggleEnableAnswerOption" title="Enabled" class="entity-action"><img alt="Enabled" height="24" src="@Url.Content("~/Content/Common/Images/Icons/accept-disabled.png")" width="24" /></button>
                    <button id="deleteAnswerOption" title="Delete" class="entity-action"><img alt="Delete" height="24" src="@Url.Content("~/Content/Common/Images/Icons/recycle-disabled.png")" width="24" /></button>
                </div>
                </text>);
            panes.Add()
                .Resizable(false)
                .Content(
                    @Html.Telerik().Grid<AnswerOptionViewModel>()
                        .Name("AnswerOptions")
                        .DataKeys(k => k.Add(o => o.ID))
                        .ToolBar(commands => commands.Insert().ButtonType(GridButtonType.ImageAndText))
                        .DataBinding(dataBinding => dataBinding.Ajax()
                            .OperationMode(GridOperationMode.Client)
                            .Select("_AnswerOptionsAjax", "Attribute", new { id = Model.ID })
                            .Update("_AnswerOptionsUpdateAjax", "Attribute", new { attributeId = Model.ID })
                            .Delete("_AnswerOptionsDeleteAjax", "Attribute", new { id = Model.ID })  
                            .Insert("_AnswerOptionsCreateAjax", "Attribute", new { id = Model.ID })    
                        )
                        .Columns(columns =>
                        {
                            columns.Bound(m => m.AnswerAbbr).Title("");
                            columns.Bound(m => m.AnswerText).Title("");
                            columns.Bound(m => m.AnswerValue).Title("").Width(32);
                            columns.Bound(m => m.RelatedImageId).Title("")
                                .ClientTemplate("<img alt='<#= AnswerText #>' class='answer-option-icon' src='"
                                    + Url.Action("AttributeAnswerOption", "ImageGenerator", new { id = Model.ID, index = 0 })
                                    + "' />").Width(32);
                            columns.Bound(m => m.RelatedImage).Hidden(); 
                            columns.Bound(m => m.Ordinal).Hidden().HtmlAttributes(new { style = "display: none;" });
                        })
                        .ClientEvents(c => c
                            .OnRowSelect("AnswerOptions_onRowSelect")
                            .OnDataBound("AnswerOptions_onDataBound")
                            .OnComplete("AnswerOptions_onComplete")
                        )
                        .HtmlAttributes(new { @class = "t-widget t-grid grid-no-header grid-no-footer" })
                        .Selectable()
                        .Editable(e => e.Mode(GridEditMode.PopUp))
                        .ToHtmlString()
                );
        })
    )



    [Key]
    [ReadOnly(true)]
    [ScaffoldColumn(false)]
    [DisplayName("Id")]
    public int ID { get; set; }

    [DisplayName("Code")]
    [StringLength(10)]
    public string AnswerAbbr { get; set; }

    [Required(AllowEmptyStrings = false)]
    [DisplayName("Value")]
    [StringLength(200)]
    public string AnswerText { get; set; }

    public int? AnswerValue { get; set; }

    [DisplayName("ImageType")]
    public LookupViewModel ImageType { get; set; }
    public string ImageTypeValue
    {
        get { return (ImageType == null) ? "" : ImageType.ColumnValue; }
        set
        {
            if (String.IsNullOrEmpty(value) == false)
            {
                var lookup = LookupViewModel.GetLookup("AnswerOption", "ImageType", value) ??
                             LookupViewModel.GetLookup("AnswerOption", "ImageType", "image/bmp");

                ImageType = lookup;
            }
            else
                ImageType = null;
        }
    }

    //[ScriptIgnore]
    public byte[] RelatedImage { get; set; }

    [DisplayName("Image")]
    public string RelatedImageId
    {
        get { return ID.ToString(); }
    }

    [Required]
    public Int16 Ordinal { get; set; }

    [Required]
    public bool Enabled { get; set; }

    public void ToggleEnabled()
    {
        Enabled = !Enabled;
    }

    //[Required]
    //public AttributeViewModel Attribute { get; private set; }

    public static AnswerOptionViewModel CreateAttributeValue()
    {
        var answerOption = new AnswerOptionViewModel();


    [GridAction]
    public ActionResult _AnswerOptionsCreateAjax(AnswerOptionViewModel model)
    {
        var answerOption = AnswerOptionViewModel.CreateAttributeValue();
        var newId = (Attribute.AnswerOptions.Count > 0) ? Attribute.AnswerOptions.Min(ao => ao.ID) - 1 : -1;
        if (newId > 0)
            newId = -1;

        answerOption.ID = newId;
        answerOption.AnswerText = "[blank]";

        if(TryUpdateModel(answerOption))
        Attribute.AnswerOptions.Add(answerOption);

        return View(new GridModel(Attribute.AnswerOptions));

    }

请帮我弄清楚发生了什么,我是MVC的新手。提前谢谢。

1 个答案:

答案 0 :(得分:2)

您无法使用insert,update,delete传递参数。您只能通过选择传递。

如果您想通过insert,update,delete传递参数,那么您必须在onsubmitchanges事件中写入网格,如下所示:

function onSubmit(e) {
        e.values.myVal = "Rahul";
    }

在上面的代码中,myVal是控制器中我想要的变量。 通过这个,您将能够获取控制器中的值。