kielou
kielou

Reputation: 437

Prevent loading page

I have a controller create and return content where it triggers an alert. The problem it load an empty page. How can I prevent it to load an empty page and stays in the current view and do nothing just pop the alert.

What I really wanted here is before the user can create new data, it will validate first if the diseaseID is already existing for specific assessmentID, and when the result is null, it will just pop an alert and do nothing. But here I'm just trying make alert() work properly.

Controller:

public ActionResult CreateDisease(int? diseaseID,int? assessmentID, DiseaseList diseaselist)
    {
        diseaselist.DiseaseID = diseaseID;
        diseaselist.AssessmentID = assessmentID;
        db.DiseaseLists.Add(diseaselist);
        db.SaveChanges();
        return Content("<script language='javascript' type='text/javascript'>alert('Item already exist');</script>");

    }

Upvotes: 0

Views: 686

Answers (1)

Ali Ashraf
Ali Ashraf

Reputation: 119

You dont need to return the script as content from the controller, just return the values as json that will tell you that the item already exist and based on that true or false value show an alert from the client side js.

your controller code will look something like this

diseaseModel.DiseaseID = diseaseID;
diseaseModel.AssessmentID = assessmentID;
diseaseModel.AlreadyExist = true;

return Json(diseaseModel);

Upvotes: 1

Related Questions