We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Grid Insert Action Method is not called

Hi,

Since my code is a bit huge, let me break it up for you:

First of all,
These are the scripts (which I always doubt that they are ordered correctly):
     ...
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>

    <script src="@Url.Content("~/Scripts/jquery.easing.1.3.min.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery.globalize.min.js")"></script>
    <script src="@Url.Content("~/Scripts/jsrender.min.js")"></script>
    <script src="@Url.Content("~/Scripts/ej/ej.web.all.min.js")"></script>

    <script src="@Url.Content("~/Scripts/ej/ej.unobtrusive.min.js")"></script>

Next,
I have a master grid (let's call it questions) and a detail grid (let's call it answers). 
    Note: each of them also has a child grid.

I'm using a DialogTemplate and a RemoteSaveAdaptor as such:
--------------------------
.Datasource(ds =>
    {
        ds.Json((IEnumerable<object>)ViewBag.Questions)
            .InsertURL("InsertQuestion")
            .UpdateURL("UpdateQuestion")
            .RemoveURL("DeleteQuestion")
            .Adaptor(AdaptorType.RemoteSaveAdaptor);
    })
-------------------------- (And the same thing for answers)

Also note that I am not directly performing CRUD operations on the webservice, instead I am caching every operation, and when the user presses on the submit button I am inserting all the data to the database.

Problem:
When I add an answer for an existing question, the Action is successfully called, but the moment I create a new question, the Answer action is never called afterwards neither on the newly inserted question nor on the existing questions.

These are the Answers Columns:
-------------------------------------
.Columns(col =>
        {
            col.Field("QuestionId").HeaderText("Question Id").Width(50)
                .AllowEditing(false)
                .Add();
            col.Field("AnswerId").HeaderText("Answer Id").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(30)
                .AllowEditing(false)
                .IsIdentity(true)
                .Add();
            col.Field("IsCorrect").Width(50)
                .EditType(EditingType.Boolean)
                .Add();
        })
-------------------------------------

And this is the template:
-------------------------------------
<!-- Answer Template -->
    <script type="text/template" id="AnswerTemplate">
        <div class="dialog-small">
            <table border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <th>Answer Id</th>
                    <td style="text-align: left">
                        <input id="Answer_AnswerId" name="AnswerId" value="{{: AnswerId }}" class="e-field e-ejinputtext valid e-disable" />
                    </td>
                </tr>

                <tr>
                    <th>Question Id</th>
                    <td style="text-align: left">
                        <input id="Answer_QuestionId" name="QuestionId" value="{{: QuestionId }}"
                               disabled="disabled" class="e-field e-ejinputtext valid e-disable" />
                    </td>
                </tr>

                <tr>
                    <th>Is Correct</th>
                    <td style="text-align: left">
                        <input id="Answer_IsCorrect" name="IsCorrect" value="{{:IsCorrect}}" class="e-field e-ejinputtext" />
                    </td>
                </tr>
            </table>
        </div>
    </script>
-------------------------------------

Javascript:
-------------------------------------
/* Answer Template */
function answersActionComplete(args) {
    var addMode = args.requestType == "add";
    var editMode = args.requestType == "beginedit";

    if ((addMode || editMode) && args.model.editSettings.editMode == "dialogtemplate") {
        $("#Answer_AnswerId").ejNumericTextbox({
            value: addMode ? nextAnswerId : $("#Answer_AnswerId").val(),
            enabled: false
        });

        $("#Answer_QuestionId").ejNumericTextbox({
            value: selectedQuestionId,
            enabled: false
        });
        $("#Answer_QuestionId").validate();

        console.log($("#Answer_IsCorrect").val());
        $("#Answer_IsCorrect").ejCheckBox({
            checked: $("#Answer_IsCorrect").val()
        });
    }
-------------------------------------
Another Question:
I have a checkbox in the Answer template (as seen above), when I add an answer & check it and save, it remains unchecked when added to the grid, Also the same happens when i edit, something is wrong in the binding of the checkbox although when i log the state of the checkbox it shows ($("#Answer_IsCorrect").val() = true), but it doesn't actually show that it is checked.

Let me know if you need anything specific for investigation,
Best Regards

8 Replies

AC Abed Chehab July 23, 2015 02:47 PM UTC

I added $("#Answer_QuestionId").validate(); by mistake, I should have removed that, but this has nothing to do with the problem.

Thanks


RU Ragavee U S Syncfusion Team July 27, 2015 12:49 PM UTC

Hi Abed,

Thanks for your interest in Syncfusion products.

Query #1: These are the scripts (which I always doubt that they are ordered correctly):

The order by which you have referred the scripts files is correct. If you have any doubt later, you can refer the below link for reference purposes.

Online Documentation Link: http://helpjs.syncfusion.com/js/control-initialization

Query #2: When I add an answer for an existing question, the Action is successfully called, but the moment I create a new question, the Answer action is never called afterwards neither on the newly inserted question nor on the existing questions

We are sorry to let you know that we are quite unclear on your above requirement. So can you please provide us more and clear information on your requirement.

As you have mentioned that the Answer action is not triggered when creating a new question, could you please tell us if you are updating the master grid dataSource also when updating the child grid dataSource or vice versa.

Also, please share your controller and view code with us if possible which will be helpful for us to analyze and assist you accordingly.

For your convenience, we have created a Master-Detail sample with Dialog template editing and remoteSaveAdaptor. The sample can be downloaded from the below location.

Sample Link: http://www.syncfusion.com/downloads/support/forum/119709/ze/Sample-2087724626

Query #3: I am not directly performing CRUD operations on the webservice, instead I am caching every operation, and when the user presses on the submit button I am inserting all the data to the database

We suspect that you are getting the changes; storing in a temporary variable and saving the changes to the database through an external button action. If you want to use DialogTemplate mode, then performing the action externally as mentioned is correct. Else, we suggest you to make use of our batch editing mode in which the add, edit, delete operations are handled in a single post action. Please refer the below online sample for more information.

Online Sample Link: http://mvc.syncfusion.com/demos/web/grid/batchediting

Query #4: I have a checkbox in the Answer template (as seen above), when I add an answer & check it and save, it remains unchecked when added to the grid

We have analyzed the above query and the code snippets that you have shared with us.

In the code corresponding to the template, we found that the “name” value differs with that of the field name and thus the entered value will not be saved in the database.

So we suggest you to specify the “name” attribute value in the template as same as that of the field name such that the value gets saved. Please refer the below code snippet.

[Incorrect code]

<td style="text-align: left">

                <input id="Answer_IsCorrect" name="IsCorrect" value="{{:IsCorrect}}" class="e-field e-ejinputtext" />
            </td>

[Correct code]

<td style="text-align: left">

                <input id="Answer_IsCorrect" name="Answer_IsCorrect" value="{{:IsCorrect}}" class="e-field e-ejinputtext" />
            </td>


Query #5: when i edit, something is wrong in the binding of the checkbox although when i log the state of the checkbox it shows ($("#Answer_IsCorrect").val() = true), but it doesn't actually show that it is checked.

When editing the record, in the actionComplete event of the grid, we found that you have assigned the value of the checkbox with $("#Answer_IsCorrect").val() , which is not the proper way. We suggest you to render the checkbox initially in template using jsrender condition. Please refer the below code snippet.

<td style="text-align: left">

        {{if Answer_IsCorrect}}

        <input id="Answer_IsCorrect" name="Answer_IsCorrect" type="checkbox" checked="checked" />

        {{else}}

        <input id="Answer_IsCorrect" name="Answer_IsCorrect" type="checkbox" />

        {{/if}}
</td>


Please get back to us if you need any further assistance.

Regards
Ragavee U S


AC Abed Chehab July 27, 2015 01:22 PM UTC

Hello,

As for the Grid, although the requirements have changed, thank you for the "batch editing" tip I will certainly be using that.

For the other questions, you have answered them all, thank you for your great support!

Best regards.


RU Ragavee U S Syncfusion Team July 28, 2015 11:51 AM UTC

Hi Abed,

Thanks for your appreciation.

We are happy that your requirements are met.
 
If you have any queries in the future, please get back to us. We will be happy to assist you.
Regards
Ragavee U S



AC Abed Chehab July 30, 2015 02:03 PM UTC

Hello again,

After a few changes in the requirements, I am now using a Hierarchy Grid + Batch Editing as a first draft.

Parent: Question
     Child: Answer

A Question has a foreign key 'Category Id' Like this:


My first question:
I created an List<object> that has a value of 'CategoryId' and a text of 'CategoryText', however, the Text is only displayed when i am in the edit mode of the Category  cell, can i display the text in all rows like in row 1 for example?

My code looks like this:
- Controller (setting the category data)
private List<object> CategoryId {
get {
var categories = GetCategories(); // get from service
var CategoryId = new List<object>();
foreach (var item in categories)
{
     CategoryId.Add(new { value = item.CategoryId, text = item.TextFr });
}
return CategoryId
     }
}

and of course setting that list in a view data like this, ViewData["CategoryId"] = this.CategoryId;

- View (the category Column)

 col.Field("CategoryId")
                .Width(20)
                .HeaderText("Category")
                .EditType(EditingType.Dropdown)
                .DataSource((List<object>)ViewData["CategoryId"])
                .Add();

--------------------------------------------------------------------------------
My Next Question

This is the hierarchy grid, the parent is the question and the child is the answer
So in the child grid there is a QuestionId, but i'm hiding it now
When i create an answer, and save, the batchUpdate(batch Url) is called successfully, but the QuestionId is 0 and not the QuestionId of the parent grid, how can I send the correct questionId ?

I am sure in the parent grid the Question Id is retrieved correctly, and in the child grid if i get the answers they are bound correctly, but when i want to add i want to link the Question Id




The child grid: 

.ChildGrid(AnswersChild =>
        {
            AnswersChild.AllowTextWrap()
            .QueryString("QuestionId")            
            .Datasource(ds =>
            {
                ds.URL("ChildDataSource")
                .BatchURL("UpdateChild")
                .Adaptor(AdaptorType.UrlAdaptor);
            })
            .EnableHeaderHover()
            .AllowSorting()
            .AllowPaging()
            .AllowFiltering()
            .AllowTextWrap()
            .AllowResizing()
            .FilterSettings(filter =>
            {
                filter.FilterType(FilterType.Excel);
            })
            .EditSettings(edit =>
            {
                edit.AllowAdding()
                    .AllowEditing()
                    .AllowDeleting()
                    .EditMode(EditMode.Batch);
            })
            .ToolbarSettings(toolbar =>
            {
                toolbar.ShowToolbar().ToolbarItems(items =>
                {
                    items.AddTool(ToolBarItems.Add);
                    items.AddTool(ToolBarItems.Edit);
                    items.AddTool(ToolBarItems.Delete);
                    items.AddTool(ToolBarItems.Update);
                    items.AddTool(ToolBarItems.Cancel);
                });
            })
            .Columns(col =>
            {
                col.Field("AnswerId")
                    .IsPrimaryKey(true)
                    .TextAlign(TextAlign.Right)
                    .Visible(false)
                    .Add();

                col.Field("QuestionId")
                    .Visible(false)
                    .Add();

                col.Field("TextFr")
                    .HeaderText("French Text")
                    .Width(35)
                    .Add();
             .........

Regards


RU Ragavee U S Syncfusion Team July 31, 2015 04:10 PM UTC

Hi Abed,

Thanks for getting back to us.

Query #1: can i display the text in all rows like in row 1 for example?

We have analyzed the above query and the code snippets that you have shared with us. In order to achieve your requirement, we suggest you to use the foreignKey feature of the grid.

For your convenience, we have created a sample based on your requirement and the same can be downloaded from the below location.

Sample Link: http://www.syncfusion.com/downloads/support/forum/119709/ze/Hierarchy_Sample680951067

In the above sample, we have a foreignKey column “ReportsTo” that binds to the Employee Table. Thus with “ReportsTo” as value, we have displayed the “FirstName” of the employee in the grid. Please refer the below code snippet.

@(Html.EJ().Grid<object>("ParentGrid")

            .Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.Master)

                        .BatchURL("/Home/BatchUpdateParent")

                        .Adaptor(AdaptorType.RemoteSaveAdaptor))

            . . . . .           

            .Columns(col =>

            {

                col.Field("EmployeeID").HeaderText("Employee ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Add();

                . . . . .

               col.Field("ReportsTo").HeaderText("Employee Name").ForeignKeyField("ReportsTo")

               .ForeignKeyValue("FirstName").DataSource((IEnumerable<object>)ViewBag.Master).Add();              

            })

            . . . .
)

[In controller]

public ActionResult Index()

        {           

            var DataSource1 = EmployeeRepository.GetAllRecords().ToList();

            ViewBag.Master = DataSource1;           

            return View();
        }


Please refer the below online sample link and documentation link for more information on the ForeignKey feature of the grid.

Online Sample Link: http://mvc.syncfusion.com/demos/web/grid/foreignkeycolumn

Online Documentation Link: http://help.syncfusion.com/ug/aspnetmvc/index.html#!Documents/foreignkeycolumns.htm

Query #2: When i create an answer, and save, the batchUpdate(batch Url) is called successfully, but the QuestionId is 0 and not the QuestionId of the parent grid, how can I send the correct questionId ?

We have analyzed the above query and we have considered this query “Filtered queryString value not passed to controller by default when adding” as a bug and a support incident has been created under your account to track the status of this issue. Please log on to our support website to check for further updates.

https://www.syncfusion.com/account/login?ReturnUrl=/support/directtrac/incidents

As of now, we have handled the issue using the below workaround. Please refer the code snippets.

<div style="margin-top:30px">

    @(Html.EJ().Grid<object>("ParentGrid")

            . . . .           

               .ChildGrid(childnew =>

               {

                   childnew.Datasource(ds => ds.URL("/Home/ChildData")

                     .BatchURL("/Home/BatchUpdateChild")

                     .Adaptor(AdaptorType.UrlAdaptor))

                     .QueryString("EmployeeID")

                     . . . . .

                    .ClientSideEvents(c => c.BeforeBatchSave("batchSave"));

    })

)

</div>


<script type="text/javascript">

  

    function batchSave(args) {

        for (var i = 0; i < args.batchChanges["added"].length; i++) {

            args.batchChanges["added"][i][args.model.queryString] = args.model.parentDetails.parentKeyFieldValue;

        }       

    }
</script>


In the above workaround, we have explicitly added the QueryString column value to the added record on the BeforeBatchSave event of the grid.

Please try the above workaround and get back to us if you need any further assistance.

Regards
Ragavee U S


AC Abed Chehab August 4, 2015 08:16 AM UTC

Hi Ragavee,

Thank you so much for your solutions.

Best regards


RU Ragavee U S Syncfusion Team August 5, 2015 04:24 AM UTC

Hi Abed,

Thanks for your update.

We are happy that your requirements are met.
 
If you have any queries in the future, please get back to us. We will be happy to assist you.
Regards
Ragavee U S

Loader.
Live Chat Icon For mobile
Up arrow icon