Home > Mobile >  mvc notification when record is added
mvc notification when record is added

Time:10-26

Using a asp mvc system, I would like to perform some kind of user notification that a record was added into the database.

Current paradigm... navPage -> modPage -> datagridPage -> newDatabasePage -> (record added action) This is where it stops, the user is not notified that record is added or failure, it simply reloads the view.

Currently - i am using some text in a viewbag variable after the record was added and based on this value, display a simple javascript message box. But I think there is a better way.

Was thinking that a modal popup implemented through jquery would accomplish this task, but i was educated this was not an optimal solution without using a messaging framework (signalR etc).

Another approach was to use an additional partial page - another partial page????

Any different options that i have missed here would be greatly appreciated.

CodePudding user response:

You can add some beautiful notifications using sweetalert2, here is the documentation

CodePudding user response:

Are you using Bootstrap (or similar)?

Can you not just pass some information into a hidden-as-default alert div?

Assuming you're using EF, when you save an object you can capture the new Id so you can just check for this.

Instead of reloading the view, redirect to the action that creates the initial view and change it so you can pass in the new Id:

int newId = _context.SaveChanges();

return RedirectToAction("GridView", new { Id = newId });

Then change the signature of the Action:

public async Task<ActionResult> GridView(int? Id) 

You can then just check if the Id has a value and do something with that information. Then just add that to the ViewModel for the page and display the alert if required:

if (Id.HasValue)
{                
    viewModel.displayAlert = true;                
}

If you don't want to reload the view in full then you can accomplish the same thing using Ajax calls.

  • Related