This is a migrated thread and some comments may be shown as answers.

Update and Delete Action are not firing after adding new record using ViewModel and multiple grid

7 Answers 277 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Cheng-Chang
Top achievements
Rank 1
Cheng-Chang asked on 26 Oct 2016, 09:12 AM

When I create a new record in grid, it works. But after that, I do delete or update action, it will be trigger as a create action. If I reload the whole page, those action is regular. this situation ONLY happens when I create records and then edit them. It seems like that grid doesn't get refresh. I have no idea about why Create controller does not return a proper response.

 

My code as follow:

[client side]

@model TYHD.ViewModels.TREATMENT

[[First Kendo Grid]]
@(Html.Kendo().Grid(Model.CVVHF.Details)
.Name("CVVHFGrid")
.Columns(columns =>
{
    columns.Command(cmd => cmd.Edit().Text("編輯").UpdateText("更新").CancelText("取消")).Width("100px").Locked(true);
    columns.Command(cmd => cmd.Destroy().Text("刪除")).Width("100px").Locked(true);
    columns.Bound(p => p.TIME).Width("150px").Locked(true).Column.Title = "時間(HHMM)";
    columns.Bound(p => p.ITEM_01).Width("180px").Column.Title = "流速(cc/min)";
    columns.Bound(p => p.ITEM_02).Width("180px").Column.Title = "流量(cc/hr)";
    columns.Bound(p => p.ORIGINAL_TIME).Hidden();
})
.Scrollable()
.ToolBar(toolbar => toolbar.Create().Text("新增"))
.Editable(ed => ed.Mode(GridEditMode.PopUp))
.DataSource(dataSource => dataSource
    .Ajax()
    .Model(model =>
    {
        model.Id(p => p.TIME);
        model.Field(p => p.TIME).DefaultValue(DateTime.Now.ToString("HHmm"));
    })
    .Update(update => update.Action("Treatment", "Record", new { FCode = "CVVHFU", HDREC_ID = hdrec_id, FORM_ID = "CVVHF" }))
    .Destroy(update => update.Action("Treatment", "Record", new { FCode = "CVVHFD", HDREC_ID = hdrec_id, FORM_ID = "CVVHF" }))
    .Create(update => update.Action("Treatment", "Record", new { FCode = "CVVHFC", HDREC_ID = hdrec_id, FORM_ID = "CVVHF" }))
))

[[Second Kendo Grid]]
@(Html.Kendo().Grid(Model.PPDFP.Details)
.Name("PPDFPGrid")
.Columns(columns =>
{
    columns.Command(cmd => cmd.Edit().Text("編輯").UpdateText("更新").CancelText("取消")).Width("100px").Locked(true).Lockable(false);
    columns.Command(cmd => cmd.Destroy().Text("刪除")).Width("100px").Locked(true).Lockable(false);
    columns.Bound(p => p.TIME).Width("150px").Locked(true).Lockable(false).Column.Title = "時間(HHMM)";
    columns.Bound(p => p.ITEM_01).Width("180px").Column.Title = "流速(cc/min)";
    columns.Bound(p => p.ITEM_02).Width("200px").Column.Title = "TMP(kpa)";
    columns.Bound(p => p.ORIGINAL_TIME).Hidden();
})
.Scrollable()
.ToolBar(toolbar => toolbar.Create().Text("新增"))
.Editable(ed => ed.Mode(GridEditMode.InLine))
.DataSource(dataSource => dataSource
    .Ajax()
    .Model(model =>
    {
        model.Id(p => p.TIME);
        model.Field(p => p.TIME).DefaultValue(DateTime.Now.ToString("HHmm"));
    })
    .Update(update => update.Action("Treatment", "Record", new { FCode = "PPDFPU", HDREC_ID = hdrec_id, FORM_ID = "PP-DFP" }))
    .Destroy(update => update.Action("Treatment", "Record", new { FCode = "PPDFPD", HDREC_ID = hdrec_id, FORM_ID = "PP-DFP" }))
    .Create(update => update.Action("Treatment", "Record", new { FCode = "PPDFPC", HDREC_ID = hdrec_id, FORM_ID = "PP-DFP" }))
))

 

[[Server side]]

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Treatment([DataSourceRequest] DataSourceRequest request, Details dtl)
{
    switch (Request["FCode"].ToString())
    {
        case "CVVHDC":
        case "PPDFPC":
           //do create
            break;
        case "CVVHDU":
        case "PPDFPU":
           //do update
           break;
        case "CVVHDD":
        case "PPDFPD":
              //do delete
              break;
      }
     return View(dtl);
}

[[ViewModel]]
public class TREATMENT
{
    public string aaa { get; set; }
    public string bbb { get; set; }
    public Master CVVHF { get; set; }
    public Master PPDFP { get; set; }
}
public class Master
{
    public string FORM_ID { get; set; } = "";
    public string ccc { get; set; } = "";
    public string ddd { get; set; } = "";
    public List<Details> Details { get; set; }
}
public class Details
{
    public string ORIGINAL_TIME { get; set; } = "";
    public string TIME { get; set; } = "";
    public string ITEM_01 { get; set; } = "";
    public string ITEM_02 { get; set; } = "";
}

7 Answers, 1 is accepted

Sort by
0
Stephen
Top achievements
Rank 2
answered on 26 Oct 2016, 01:43 PM

I don't think you are returning the correct response from your server.

Take a look at the inline editing demo(http://demos.telerik.com/aspnet-mvc/grid/editing-inline) and look at what the server is returning for each of the Create, Update, and Destroy cases.

 

Most importantly, in the Create case, you *must* return the created object *with its ID field set*(which is TIME in your case).

The grid triggers on whatever field you have configured as the ID(model.Id(p => p.TIME)).

If that field is 0/blank/null, the row is considered new.  If you do not return the created object with TIME set, the grid will still consider it new.  By returning the proper response with the TIME set, the grid will essentially update that row from "new" to "existing".

 

You are just returning some View as the response to grid's create/update/destroy...it likely does not understand the response at all.

0
Cheng-Chang
Top achievements
Rank 1
answered on 27 Oct 2016, 12:29 AM

Thanks for your reply.

I have tried return Json(new[] { dtl}.ToDataSourceResult(request, ModelState));

But it still doesn't work.

0
Stephen
Top achievements
Rank 2
answered on 27 Oct 2016, 12:47 PM

In your // do create, what is being returned?  This *must* set the TIME field of the dtl object before you pass it back.

Are there any errors in the JS console?

 

The only other thing I can suggest is to step through the inline editing demo in the examples solution (<Kendo Install Folder>\wrappers\aspnetmvc\Examples) and explore what it is doing and figure out where yours differs.

 

 

0
Cheng-Chang
Top achievements
Rank 1
answered on 28 Oct 2016, 08:38 AM

I return Json(new[] { dtl}.ToDataSourceResult(request, ModelState));  and TIME field is included in dtl. There is no error in JS console.

I review my code, and check details but the example code is not different with mine.

0
Stephen
Top achievements
Rank 2
answered on 28 Oct 2016, 12:57 PM

So, I can't tell you why, but using TIME(string) as the Model.Id does not work.

If just threw your code into a clean project and duplicated your problem.

But, if you add and integer ID field to your Detail model and then set it to a non-zero value on Create, it *does* work, i.e. editing the new item will properly trigger the Update action.

 

0
Accepted
Stephen
Top achievements
Rank 2
answered on 28 Oct 2016, 01:04 PM

OK...I played around some more and I know why TIME does not work as the Model.Id.

 

The problem is that your TIME field is getting set to DateTime.Now.ToString("HHmm") when added to the grid.

So, when you submit the Create, TIME(your Id) is already set and when it comes back from the controller, it has not changed, so I guess the grid thinks it has not been saved as the Model.Id before and after the Create action is the same.

 

If you comment out the model.Field(p => p.TIME).DefaultValue(DateTime.Now.ToString("HHmm")); in the model definition and instead assign the value of TIME on the server Create action, it should work.

0
Cheng-Chang
Top achievements
Rank 1
answered on 31 Oct 2016, 07:06 AM

Dear Stephen:

Thanks a lot. I was stuck in this situation for many days, and I never thought it will be effect by default value setting. According to your suggestion, I modified my code, and it works now. Thanks again. 

Tags
General Discussions
Asked by
Cheng-Chang
Top achievements
Rank 1
Answers by
Stephen
Top achievements
Rank 2
Cheng-Chang
Top achievements
Rank 1
Share this question
or