Error with ViewResult and ActionResult containing the same parameters

In my controller, I have an Edit GET method to display the view and an Edit POST method to save the changes:

public ViewResult Edit(int id) { // } [HttpPost] public ActionResult Edit(int id) { // } 

But I get an error message:

The type 'Controllers.MyController' already defines a member named Edit with the same parameters

How do I get around this?

+4
source share
7 answers

You can implement view models, so you have an EditViewModel containing all the fields you want, so that the user can edit and return this to their GET editing method and have a strongly typed representation of the view model. Then this means that in your POST method, you will pass EditViewModel as a parameter, a bit like this:

 [HttpGet] public ViewResult Edit(int id) { //build and populate view model var viewModel = new EditViewModel(); viewModel.Id = id; viewModel.Name = //go off to populate fields return View("", viewModel) } [HttpPost] public ActionResult Edit(EditViewModel viewModel) { //use data from viewModel and save in database } 

And so your GET and POST methods will have different signatures. Hope this helps.

+5
source

You should read this ( 3.6 Signatures and Overloading ) about function overloading.

Function overload

In this approach, you can have two or more functions with the same name. But each function must have different signatures (i.e. different types of parameter, sequence of parameters or number of parameters).

Note: return type is not a parameter signature

In your code, you implemented both functions with the same name and signatures.

+3
source

For another, less elegant solution, imagine a site with a โ€œWizard-likeโ€ page structure (Views), where you want to transfer the ViewModel from page 1 to page 2, from Page 2 to page 3, etc.

The problem is that the GET version must get the model from page 1, but it must also pass the model to perform a postback. Therefore, both the GET and POST versions of any "medium" pages need a signature that contains the model.

The workaround is to simply add the "junk email parameter" to the signature, making sure it is NULL using the parameter.

  [HttpGet] public ActionResult Page2(MyModel myModel) { } [HttpPost] public ActionResult Page2(MyModel myModel, int? i) { } 
+2
source

This is because you are passing the same parameter to both functions that are not allowed, although you are specifying HttpPost on one. You can change the name of the "Edit Message" function and specify it in Html.BeginForm () or change the parameter in FormCollection instead of int

+1
source

I think the easiest way to do this is to add an additional optional parameter to the global.asax.cs file:

 new { controller = "Home", action = "Index", id = UrlParameter.Optional, id2 = UrlParameter.Optional } // Parameter defaults 

and change the second function from

 [HttpPost] public ActionResult Edit(int id) 

to

 [HttpPost] public ActionResult Edit(int id, int id2) 

Thus, you do not need to change any logic. Since the second parameter is optional. He will not complain if you do not provide value.

0
source

If you use the View Model on your POST controller method, make sure your model has an empty constructor. It drove me crazy.

 namespace app.Models { public class UserEdit { public User User { get; set; } public UserEdit() { } } } 
0
source

You can try it instead.

  public ActionResult Edit() { return View(); } [HttpPost] [ActionName("Edit")] public ActionResult EditPosted() { return View(); } 
0
source

Source: https://habr.com/ru/post/1335643/


All Articles