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.