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

How to Display Server Message using PopUp Editing

1 Answer 290 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Phillip
Top achievements
Rank 1
Phillip asked on 24 Aug 2018, 07:42 PM

I am using the grid with pop up editing. When a record is being created or updated we perform some validation logic to make sure a record with the same name value is not already present. If one if found we do not create or update the record and need to alert the user of this. I've looked for this but everything I've found deals with if an error has occurred and in this case no error occurs but the code simply does not continue.

public ActionResult Classification_Create([DataSourceRequest] DataSourceRequest request, Classification classification)
        {
            if (classification != null && ModelState.IsValid)
            {
                this.Create(classification);
            }
            return Json(new[] { classification }.ToDataSourceResult(request, ModelState));
        }
 
 private void Create(Classification classification)
        {
            try
            {
                // Check for duplicate before adding
                var classificationCheck = _db.Classifications.FirstOrDefault(x => x.Name == classification.Name && x.DivisionId == classification.DivisionId);
 
                if (classificationCheck == null)
                {
                    classification.Active = true;
                    _db.Classifications.Add(classification);
                    _db.SaveChanges();
 
                    // Log the creation
                    Logging.CreateLog("Classification", classification.Id, "New classification record created", PlatformSession.CurrApplications[PlatformApplicationSession.OperationHighlights]);
                }
                else
                {
                    // Check to see if it's just makred as inactive and return a message to user
                    string errorMessage = !classificationCheck.Active ? "A classification with this name already exists but is simply in-active. Please locate this classification and re-activate it." : "A classification with this name already exists for this division. Please check the data again and re-try.";
                    ViewBag.Error = errorMessage;
                }
            }
            catch (Exception ex)
            {
                // Log the error
                ExceptionHandling.CreateErrorLog(ex, PlatformSession.CurrApplications[PlatformApplicationSession.OperationHighlights]);
                // Send the user to a notification screen.
                string errorMessage = "There was an issue creating the routing template and this has been logged. Please try again but if this persists please contact a platform administrator.";
                ViewBag.Error = errorMessage;
            }
        }

 

In the Create method we perform a simply check to see if this record exists. If so we do not create the record and place a message into a ViewBag object. In the current layout the pop-up window goes away and nothing has been added. I would like to have the window stay in place and another window appear that contains the message the server placed into the ViewBag object.

 

 

1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 28 Aug 2018, 11:18 AM
Hi Phillip,

In order to implement the behavior I would suggest an approach that is a bit different. Since adding a duplicate record is an invalid operation I would suggest adding an error to the ModelState. You can use the AddModelState method for that.

Then on the client you can show the relevant error by handling the DataSource error event. To prevent the popup from closing you can use the approach described in the following thread. 


Let me know how this approach works for you.


Regards,
Viktor Tachev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Phillip
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Share this question
or