2

I want to know about a pattern to do this. I have a controller with method1()

method1(){
 return View();

}
[httpost]
method1(string something){

  return View(object);
}

[httpost]
method2(int? id){

  return redirectToaction("method1");
}

View:
<div>
beginform(){
   textfield
   submit button
}
</div>
<div>
if(viewbag.something != null){
   beginform("method2", "controller", new{id=number}){
     <span>@model.info</span>
     submit button
    }
}
</div>

and this displays a view this view has a form a text field and submit and this calls method1() but with the HTTPPOst and in the same view i display another form which will call method2() my question is how can i display a message in the view? like "user has been deleted" without having to create another view. Is there another way of doing this in asp mvc or do i have to include ajax?

I tried setting a viewBag inside method2, but since method2 redirectsaction to method1 it somehow does not stay, and it is not displayed in the view. Thanks!

3
  • So I am guess through the vagueness of your question that method1() presents a drop down list of users and method2() is information about your users. And you want "user has been deleted" to show up if they select a deleted user (that still shows up) on method1()? Commented Mar 27, 2011 at 6:41
  • OR is method2() used to delete the user and you want feedback that the user has been deleted? Commented Mar 27, 2011 at 6:42
  • method2 deletes the user and i want it to display a message in the same view, but wanted to know if i can do this on asp mvc or if i have to add ajax to asp mvc Commented Mar 27, 2011 at 7:18

1 Answer 1

3

You could use TempData which is preserved between a single redirect:

public ActionResult method1() 
{
    return View();
}

[HttpPost]
public ActionResult method1(string something)
{
    return View(something);
}

[HttpPost]
publicActionResult method2(int? id)
{
    TempData["message"] = "User has been deleted";
    return RedirectToAction("method1");
}

and in the view display the message:

<div>@TempData["message"]</div>

If there is no message in TempData it will simply display an empty div. You could further check if TempData["message"] != null in the view if you didn't want the empty div.

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

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.