Questions tagged [actionmethod]

Action methods typically have a one-to-one mapping with user interactions. Examples of user interactions include entering a URL into the browser, clicking a link, and submitting a form. Each of these user interactions causes a request to be sent to the server. In each case, the URL of the request includes information that the MVC framework uses to invoke an action method.

Most action methods return an instance of a class that derives from ActionResult. The ActionResult class is the base for all action results. However, there are different action result types, depending on the task that the action method is performing. For example, the most common action is to call the View method. The View method returns an instance of the ViewResult class, which is derived from ActionResult.

You can create action methods that return an object of any type, such as a string, an integer, or a Boolean value. These return types are wrapped in an appropriate ActionResult type before they are rendered to the response stream.

136 questions
71
votes
4 answers

How to receive JSON as an MVC 5 action method parameter

I have been trying the whole afternoon crawling through the web trying to receive a JSON object in the action controller. What is the correct and or easier way to go about doing it? I have tried the following: 1: //Post/…
Zapnologica
  • 20,003
  • 39
  • 136
  • 229
40
votes
10 answers

ASP .NET MVC NonAction meaning

Can anybody please tell me why should I use NonAction? I mean say I have a form with several submit values: Update, Delete or Insert. Since all the submit buttons have the same form in common I'm switching the submit value inside the controller and…
Shaokan
  • 6,522
  • 12
  • 52
  • 78
24
votes
5 answers

What clever things have you done with an ASP.NET MVC Action method

The ASP.NET MVC controller action methods are primarily used for handling 'business' operations but it can be used for lots more. I thought it would be fun to see what creative, useful things people have created actions for that may be practical or…
Simon_Weaver
  • 120,240
  • 73
  • 577
  • 618
22
votes
3 answers

Keeping a controller thin (too many action methods)

I'm working on my first real ASP.NET MVC project and I've noticed that the controller I've been working in is getting rather large. This seemingly goes against the best practice of keeping your controllers thin. I've done a good job keeping the…
Mayo
  • 9,758
  • 6
  • 40
  • 89
11
votes
2 answers

ASP.NET MVC: Enforce AJAX request on an action

I'm looking for a way to enforce a controller's action to be accessed only via an AJAX request. What is the best way to do this before the action method is called? I want to refactor the following from my action methods: if(Request.IsAjaxRequest()) …
Remus
  • 1,423
  • 3
  • 14
  • 24
11
votes
5 answers

ASP.NET MVC - POST Action Method with Additional Parameters from URL

With ASP.net MVC is it possible to POST a form to a controller action which includes parameters not in the form, but from the URL? For example The Action method in GroupController: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(int…
Karl
  • 659
  • 1
  • 9
  • 18
10
votes
2 answers

Is it better to return the most specific or most general type from an action method?

What are the benefits or detriments of either?
DaveDev
  • 38,095
  • 68
  • 199
  • 359
9
votes
2 answers

Disable action method from being called from the address bar

I have a method in my controller that I don't want to be called from the address bar in the browser... Is there a way to do that? Maybe some kind of annotation, modification in the route configuration? Which are my options?
amp
  • 9,710
  • 15
  • 67
  • 118
8
votes
3 answers

ASP.NET MVC: How to covert an ActionResult to string?

I would like to take an existing action method, render its return value to a string and ship it as a JSON for a response to an AJAX request. To do this, I need to render an ActionResult to a string. How do i do this? We have the opposite where we…
burnt1ce
  • 12,985
  • 28
  • 96
  • 146
6
votes
1 answer

How can I specify a namespace when calling @Html.Action(...) in ASP.NET MVC

When I try to use Html.Action in ASP.NET MVC 3 for a controller in a different namespace from the current controller I get an error : Code: @Html.Action("Foo", "Application", new { id = "FG2" }) Error : The controller for path …
Simon_Weaver
  • 120,240
  • 73
  • 577
  • 618
6
votes
3 answers

ASP.NET MVC AuthorizeAttribute passing values to ActionMethod?

I'm only a newcomer to ASP.NET MVC and am not sure how to achieve a certain task the "right way". Essentially, I store the logged in userId in HttpContext.User.Identity and have written an EnhancedAuthorizeAttribute to perform some custom…
6
votes
1 answer

Is this C# action method code actually firing a 301 redirect?

Here is the code I use when someone visits a product page on my ecommerce website. public ActionResult Details(int id, string slug) { using (var productRepository = new EfProductRepository()) { var product =…
Only Bolivian Here
  • 32,571
  • 60
  • 151
  • 250
5
votes
3 answers

How to invoke a bean action method using a link? The onclick does not work

I'm trying to implement a list of users names which can be rearranged by clicking on UP or DOWN links.
5
votes
1 answer

How should I return an Image from a controller action c# asp.net-mvc-2?

I am building images from a byte[] like below. public FileContentResult GetEmployeeImage(int empId) { MemoryStream ms = new MemoryStream(byteArray); Image returnImage = Image.FromStream(ms); return returnImage;//How should i return this…
shane87
  • 2,920
  • 10
  • 48
  • 64
5
votes
1 answer

Action method being called multiple times

I've just spotted something quite strange about an action method that is being called, via a simple click of a link, in my MVC 3 site. For some reason this action method is being called multiple times. Not only that but subsequent calls get called…
Sachin Kainth
  • 41,237
  • 78
  • 185
  • 289
1
2 3
9 10