Jamie
Jamie

Reputation: 1

Telerik MVC Insert Command Not Firing Controller

I am hoping that someone will be able to help me figure out what is going wrong. I can't get my insert command to fire the controller and can't figure out why it won't. Here is my code:

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

    }

Please help me figure out what is going on, I am new to MVC. Thanks in advance.

Upvotes: 0

Views: 492

Answers (1)

Rahul
Rahul

Reputation: 21

You could not pass parameters with insert, update, delete. You can only pass with select.

And if you want to pass parameters with insert, update, delete then you have to write grids onsubmitchanges event as below:

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

In this above code myVal is the variable which I want in controller. with this you will be able to fetch the value in controller.

Upvotes: 2

Related Questions