Can we change Action Method name in ASP.NET MVC Application? - c#

This is two action method in controller with same name, i want to change this action name using attribute.
[HttpGet]
public ActionResult Show()
{
return View();
}
[HttpPost]
public ActionResult Show(FormCollection frm)
{
return View();
}

You can use ActionName attribute.
[HttpPost, ActionName("Show")]
public ActionResult PostShow()
{
// your code...
}

You can have the same name, but make sure that the method signature is different. To do that, you can simply add a parameter to your post method.
[HttpGet]
public ActionResult Show()
{
return View();
}
[HttpPost]
public ActionResult Show(string name)
{
return View();
}
Now when the Show form is submitted the input field with name value name will be submitted to the HttpPost action method.

Related

Set AntiForgeryToken for redirect to HttpGet method in C#

I want to check for CSRF in my webapp using the [ValidateAntiForgeryToken] attribute. The thing is, I have a GET method that changes data, so it should actually be a POST. But aside from a submit to this action method in the corresponding form, there are multiple redirects to this method, so I can't change it to a POST method. As far as I know, the [ValidateAntiForgeryToken] doesn't work for GET methods. Is there another way to validate a GET method or to redirect within the code to a POST method without using a form?
My action method looks something like this:
public ActionResult SomeAction(SomeModel model)
{
// changes are made in database!!
return View("View", model);
}
And this action method is being redirected to from another action method:
public ActionResult SomeOtherAction()
{
return RedirectToAction("SomeAction", "Controller");
}
I would like to change the first action to:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SomeAction(SomeModel model)
{
// changes are made in database!!
return View("View", model);
}
and add #Html.AntiForgeryToken in the corresponding view. But then the redirect in the second action won't work anymore. Does anyone know a way out of this?
You could return the someAction method...
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
[ValidateAntiForgeryToken]
public ActionResult SomeOtherAction()
{
return SomeAction();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SomeAction()
{
return View("SomeAction");
}
}
Index.cshtml
#using (Html.BeginForm("SomeOtherAction", "Home", FormMethod.Get))
{
#Html.AntiForgeryToken();
<button type="submit">Click Me</button>
}

How to have multiple aliases for a controller action

I have a Search controller with a generic Search() action that takes several parameters and has a bunch of logic. I'd like to call this from other controllers without having a lot of copy/paste code.
I'd like to call this action from these different urls/controllers.actions.
/Search/Search?text=mySearchText
/User/SearchTransactions?type=purcahse
/Transactions/UserSearch?UserId=1
I could move the method to a baseController but I'd have to call /Search on each controller and I'd like to have them all named differently.
you already have your solution man put it in base controller and decorate it with actionName attribute
like
[ActionName("Search1")]
public ActionResult SearchText(string text) {
return View();
}
[ActionName("Search2")]
public ActionResult SearchType(string Type) {
return View();
}
[ActionName("Search3")]
public ActionResult searchId(int ID) {
return View();
}
now you can do like
/search1
/search2
/search3

Return to a named view doesn't work after form sumbit

After POSTing to a server side method, I want to return the user back to the view from which he came, which was:
.../BrokerDashboard/Profile/Index
Here's the method:
[HttpPost]
public ActionResult Profile(ProfileModel Model)
{
// do stuff
return View("Index", Model);
}
There is logic in here that needs to execute after the form is posted:
public ActionResult Index(string contactid)
{
// do stuff
}
But after the post, the browser ends up here:
.../BrokerDashboard/Profile/Profile
Which means public ActionResult Index() does not get called again after the post.
I think what your looking for is RedirectToAction() It causes a redirect and will fire your Index controller method.
RedirectToAction with parameter
https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoaction(v=vs.118).aspx
[HttpPost]
public ActionResult Profile(ProfileModel Model)
{
// do stuff
return RedirectToAction("Index", Model);
}

RedirectToAction with a parameter not working

i am trying to redirect to another action within the same controller
action is called index
[HttpGet]
public ActionResult Search(string city)
{
return RedirectToAction("Index", "Rentals", new { CityName = city });
}
this is index action
[HttpPost]
public ActionResult Index(String CityName)
{
}
am i missing something?
You are trying to redirect action which is searching for a matching action but in this case there is no get action, so you have to add a get method to accept redirect. If you want, you can check the HTTPGET or POST inside the method
[HttpPost]<---- Remove this
public ActionResult Index(String CityName)
{
}
Please change HttpPost to HttpGet
[HttpGet]
public ActionResult Index(String CityName)
{
}
Because whenever you call the Action, then the GET method will be first called.
As the two actions are in the same controller, you could call the Index method directly from Search like this:
return Index(city);
not necessarily to user the RedirectToAction method.

RedirectToAction isn't working correctly

I have a simple MVC2 app that doesn't seem to Redirect correctly. The code is setup as follows:
[HttpPost]
[Authorize]
public ActionResult QuickAddEvent(CalendarEvent calEvent)
{
if (ModelState.IsValid)
{
int eventID = repo.AddEvent(calEvent);
return RedirectToAction("Event", new { id = eventID });
}
return RedirectToAction("Index", "Home");
}
[ChildActionOnly]
public ActionResult QuickAddEvent()
{
return PartialView();
}
public ActionResult Event(int id)
{
CalendarEvent curEvent = repo.ByID(id);
return View(curEvent);
}
The problem I am running into is that no matter what the ModelState is on HttpPost the page redirects to itself. That is, no matter what the model state is, I always end up at /EventCalendar/Index instead of one of the two specified actions.
Since QuickAddEvent is returning a PartialView, the form action isposting to /EventCalendar/Index and not /EventCalendar/QuickAddEvent. The fix is changing the action name for the [httpPost] to index

Categories

Resources