ASP.NET MVC 1.0 Controller Action With Same Signature - asp.net-mvc

So basically in my UserController.cs class I have an Index method that returns the ActionResult to display the dashboard for the user. On this page is a html button with a type of submit. When I hit this button I want to capture it on the server side log the user out.
How can I do this since I'm not passing information back and the method signature ends up being the same.
Thanks,
Mike
[Authorize]
public ActionResult Index()
{
return View();
}
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}

Use ActionNameAttribute:
[AcceptVerbs(HttpVerbs.Get), ActionName("Index"), Authorize]
public ActionResult IndexGet()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post), ActionName("Index"), Authorize]
public ActionResult IndexPost()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}

Use:
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection values)
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
Or
[Authorize]
[AcceptVerbs(HttpVerbs.Post), ActionName("Post")]
public ActionResult IndexPost()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}

As you know in C# you cannot have two methods with the same name and same arguments within the same class. You could either add some dummy parameter or I would advice you to rename the second action to SignOut which seems more semantically correct and better reflecting what this action actually does.

Related

Redirect from a [HttpGet] Action To [HttpPost] Action - MVC3

I want to redirect to a [HttpPost] Action from a [HttpGet] Action in the same controller.
Is it possible?
I'm trying to not rewrite the same code for a Login, because it is a complex, and not typical logon function
This is my code:
[HttpGet]
public ActionResult Login(){
return View();
}
[HttpPost]
public ActionResult Login(LoginModel model)
{
//search user and create sesion
//...
if (registered)
{
return this.RedirectToAction("Home","Index");
}else{
return this.RedirectToAction("Home", "signIn");
}
}
[HttpGet]
public ActionResult signIn(){
return View();
}
[HttpGet]
public ActionResult signInUserModel model)
{
//Register new User
//...
if (allOk)
{
LoginModel loginModel = new LoginModel();
loginModel.User = model.User;
loginModel.password = model.password;
//next line doesn't work!!!!!
return this.RedirectToAction("Home","Login", new { model = loginModel);
}else{
//error
//...
}
}
Any help would be appreciated.
Thanks
You can return a different View from the method name
public ActionResult signInUserModel model)
{
...
return View("Login", loginModel);
}
Bit late, but I just had the same issue...
You could refactor the core login logic out from the Post method and then call that new method from both places.
e.g. Suppose you create a LoginService class to handle logging someone in. It's just a case of using that from both your actions, so no need to redirect from one action to the other
It is possible. All actions must be in the same controller.
public ActionResult Login(){
return View();
}
[HttpPost]
public ActionResult Login(LoginModel model)
{
//search user and create sesion
//...
if (registered)
{
return RedirectToAction("Index");
}
return View(model);
}
public ActionResult SignIn(){
return View();
}
[HttpPost]
public ActionResult SignIn(UserModel model)
{
//Register new User
//...
if (allOk)
{
return RedirectToAction("Login");
}
return View(model);
}

RedirectToAction not return View

I am trying to redirect to action and get a new view (a new page) with no success. While debugging, I'm reaching the controller but not getting the view (the page URL is not changed).
With Fiddler I see that the page returns the right view result but in the browser the URL is not changed!
When shopping cart is empty, I would like to redirect to a new page a display the error message.
[HttpPost]
public RedirectToRouteResult PlaceOrder(DeliveryDetails deliveryDetails)
{
if (UserCart.IsEmpty)
{
TempData["errorMsg"] = "Error: Cart is empty";
return RedirectToAction("Index", "Error");
}
else
{
InsertOrder();
}
}
ErrorController:
public ActionResult Index()
{
return View();
}
ErrorController View:
#TempData["errorMsg"]
Thanks.
Then I would surmise that UserCart.IsEmpty is evaluating to false. What does your Error Index route look like? Also, you're better off returning a base ActionResult from a controller action in case you need to return a view. Presumably there's more code in the PlaceOrder method because that won't compile as it stands
Use ActionResult instead of RedirectToRouteResult
[HttpPost]
public ActionResult PlaceOrder(DeliveryDetails deliveryDetails)
{
// .....
return RedirectToAction("Index", "Error");
}

ASP.net MVC4 Clear Model Values

So I have a simple action in my controller. The project is a MVC Mobile Application.
public ActionResult Index()
{
return View();
}
this gives an form to enter data. I then handle the data in the post back.
[HttpPost]
public ActionResult Index(ScanViewModel model)
{
if (ModelState.IsValid)
{
Scan ns = new Scan();
ns.Location = model.Location;
ns.Quantity = model.Quantity;
ns.ScanCode = model.ScanCode;
ns.Scanner = User.Identity.Name;
ns.ScanTime = DateTime.Now;
_db.Scans.Add(ns);
_db.SaveChanges();
}
return View(model);
}
I want to clear the fields in the form and allow users to enter data again. However I get the exact same values back into my inputs. How can I clear them in the controller.
Just call this.ModelState.Clear()
You should follow the PRG pattern.
Just redirect to the Action method which is meant for the Create Screen. You can use the RedirectToAction method to do so.
RedirectToAction returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action.
[HttpPost]
public ActionResult Index(ScanViewModel model)
{
if(ModelState.IsValid)
{
//Code for save here
//..............
_db.SaveChanges();
return RedirectToAction("Index","User");
}
return View(model);
}
public ActionResult Index()
{
return View();
}
Assuming your controller name is UserController.

MVC Redirect ActionResult

On a MVC controller I have the following:
public virtual ActionResult Test() {
ActionResult result = MVC.Home.Index();
return RedirectToAction(result);
return View();
}
This redirects to result. View is never returned.
Now I need to do something as follows:
public virtual ActionResult Test() {
ActionResult result = MVC.Home.Index();
MyClass.RedirectTo(result, this);
return View();
}
Where MyClass.RedirectTo method is the following:
public static void RedirectTo(ActionResult result, Controller controller) {
controller.Response.Redirect(result.ToString());
}
But in this case the redirection does not happen.
The View is returned. Does anyone knows how to do this?
Hi you have to redirect to a specific action on a specific controller
e.g
return RedirectToAction("Index", "ControllerName");

two controllers with same params

i have 2 actions
public ActionResult FilesAdd(int id)
{
FillParentMenuDDL(id);
return View();
}
[HttpPost]
public ActionResult FilesAdd(int id)
{
//some logic...
FillParentMenuDDL(id);
return View();
}
but it is error because of same parameters, but i need only one parameter. first i call page /action/id and then i submit it for example with id and uploaded file, but i access to file using request.files[0]. so what the solution with controllers and same parameters? i see only declare FilesAdd(int? id) in one controller
.Net MVC has an ActionNameAttribute for this purpose. Rename your second action to something like FilesAddPost and then use ActionNameAttribute("FilesAdd")
public ActionResult FilesAdd(int id)
{
FillParentMenuDDL(id);
return View();
}
[HttpPost]
[ActionName("FilesAdd")]
public ActionResult FilesAddPost(int id)
{
//some logic...
FillParentMenuDDL(id);
return View();
}
Add an (unused) form parameter to the POST action. That will make the method signatures different.
[HttpPost]
public ActionResult FilesAdd(int id, FormCollection form)
{
//some logic...
FillParentMenuDDL(id);
return View();
}
You can control the action of the submitted form, it doesn't have to go to the same action.
// Works under MVC 2.0
<% using (Html.BeginForm("action", "controller", FormMethod.Post)) { %>
// code
<% } %>

Resources