2

How can I display a message on the same browser after inserting a student? Currently I am using return content which navigates me to a new page, but I want to display the message on same page and stay on the same page:

Index Controller:

    public ActionResult Index()
    {
        return View(_repository.ListAll().OrderByDescending(s => s.StudentID));

    }

Controller Action:

    public ActionResult RemoveStudent(int id)
    {

        StudentDataContext student= new StudentDataContext();

        var std = student.Students.Single(s => s.StudentID == id);
        student.Students.DeleteOnSubmit(std);
        student.SubmitChanges();
        return Content("Student " + std.StudentId.ToString() + " Removed");

    }

Thanks in advance

3 Answers 3

5

Return your view, and you can store the message in a model or in your TempData.

public ActionResult RemoveStudent(int id)
{
    StudentDataContext student= new StudentDataContext();

    var std = student.Students.Single(s => s.StudentID == id);
    student.Students.DeleteOnSubmit(std);
    student.SubmitChanges();

    TempData["Message"] = "Student " + std.StudentId.ToString() + " Removed";
    return RedirectToAction("Index");
}

In your view, you can check if TempData["Message"] is not null and display it.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I forgot to mention, I have an index view where the remove action is triggered. It is this page where I want to display the message after executing RemoveStudent action
@user793468, then you can call RedirectToAction and pass in an Action/View and store the message in TempData, which will exist for a single request.
1

You could return some specific view:

public ActionResult RemoveStudent(int id)
{
    StudentDataContext student= new StudentDataContext();
    var std = student.Students.Single(s => s.StudentID == id);
    student.Students.DeleteOnSubmit(std);
    student.SubmitChanges();

    ViewBag.Message = "Student " + std.StudentId.ToString() + " Removed";

    return View();

    // or if you want to specify a view name:
    // return View("MyView");

    // and if you need to pass a model that the view expects
    // return View("MyView", someModel);
}

and inside the view:

<div>@ViewBag.Message</div>

1 Comment

Sorry, I forgot to mention, I have an index view where the remove action is triggered. It is this page where I want to display the message after executing RemoveStudent action
1

You could do it asyncronously using AJAX. You can do this using JQuery:

http://blog.bobcravens.com/2009/11/ajax-calls-to-asp-net-mvc-action-methods-using-jquery/

or an ajax form:

http://www.hanselman.com/blog/ASPNETMVCPreview4UsingAjaxAndAjaxForm.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.