MVC5 prevent modal window from popping up from controller

I’m having an issue where a modal window pops up in my browser after I return to the View from the controller. The modal window contains the html code from the Index.cshtml page.

Here is the code from my controller:

public ActionResult Save(Events changedEvent,FormCollection actionValues)
{
string action_type = actionValues["!nativeeditor_status"];

        var eventText = actionValues["text"];
        var eventStart = actionValues["start_date"];
        var eventEnd = actionValues["end_date"];

        try
        {
            switch (action_type)
            {
                case "inserted":
                    if (User.IsInRole("Admin"))
                        db.Event.Add(changedEvent);                    
                    Send(eventText, eventStart, eventEnd);
                    break;

                case "deleted":
                    //changedEvent = db.Event.SingleOrDefault(ev => ev.Id == source_id);
                    changedEvent = db.Event.SingleOrDefault(ev => ev.text == eventText);
                    db.Event.Remove(changedEvent);
                    break;

                default: // update
                    //changedEvent = db.Event.SingleOrDefault(ev => ev.Id == source_id);
                    changedEvent = db.Event.SingleOrDefault(ev => ev.text == eventText);
                    UpdateModel(changedEvent);
                    break;
            }

            db.SaveChanges();

        }
        catch (Exception)
        {

            action_type = "error";
        }

        return RedirectToAction("Index", "Home");
    }

And here is my code from my Index.cshtml page that binds to the Save method:

function init() {
var dp = new dataProcessor("/Home/Save");
dp.init(scheduler);
dp.setTransactionMode(“POST”, false);
}

I am I missing something in my Index.cshtml to handle what is returned when the Save function is called? Is there an alert in the scheduler that I have to handle/disable?

Thanks for any help you can provide!

Hello,
here is what happens in your code

  1. when you change something in scheduler (e.g. create/delete/modify event), scheduler sends a POST request to Home/Save, as defined in dataProcessor inside init() function()
  2. dataProcessor sends post request and expects the server response to match a certain format of json or xml docs.dhtmlx.com/dataprocessor__ … nsedetails
  3. Your Save action outputs the html page instead of the expected response, this line:
    return RedirectToAction(“Index”, “Home”);
  4. client-side fails to parse it as a valid response and pops up a message.

In order to fix the issue, you need to return a valid response from the Save action, it can be done by replacing your current response - return [code]RedirectToAction(“Index”, “Home”); - with this:
return Content(
new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(
new System.Collections.Generic.Dictionary<string, string>
{
{“action”, action_type},
{“tid”, eventId}
}
)
);

// will send response like this {“action”:“updated”, “tid”:“5”} - which is valid response[/code]

Please note “tid” - eventId part, if you insert event into db - you have to send new database event it back to the client. If it’s any other operation - you can either return the same id as came from the client or omit “tid” field from the response.

Thank you Aliaksandr! I changing my Save function in my controller to match your solution and that fixed it. I omitted the tid and it still inserts ok. I will test it out some more to make sure everything is working but so far so good. Thanks again! This was really on my mind the last few days :smiley:

Hi,

I omitted the tid and it still inserts ok.

If you do it, inserts will work ok but you won’t be able to update the inserted item until you reload the data (i.e. changes will be sent to the backend, but db item won’t be updated).
It would happen because client-side won’t know database id of the newly inserted item, thus no way to connect it to the database record.
So unless you clear and reload calendar after each operation, providing client-side with database id of inserted items is essential