ViewResult 和 ActionResult 包含相同参数时出错

发布于 2024-10-12 07:02:17 字数 308 浏览 5 评论 0原文

在我的控制器中,我有一个 Edit GET 方法来显示视图,还有一个 Edit POST 方法来保存更改:

public ViewResult Edit(int id)
{
    //
}

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

但我收到一条错误消息:

类型“Controllers.MyController”已经定义了一个名为“Edit”的成员' 具有相同的参数类型

我该如何解决这个问题?

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'm getting an error saying:

Type 'Controllers.MyController' already defines a member called 'Edit' with the same parameter types

How do I get around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

情绪 2024-10-19 07:02:18

您可以实现视图模型,以便 EditViewModel 包含您希望用户能够编辑的所有字段,并在 Edit GET 方法中返回这些字段,并为视图模型提供强类型视图。那么这意味着在您的 POST 方法中您将传递 EditViewModel 作为参数,有点像这样:

[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
}

因此您的 GET 和 POST 方法将具有不同的签名。希望这有帮助。

You could implement view models so you have EditViewModel containing all of the fields you wish the user to be able to edit and return this in your Edit GET method and have a strongly typed view to the view model. Then that means that in your POST method you would pass the 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 would have different signatures. Hope this helps.

风情万种。 2024-10-19 07:02:18

您必须阅读此内容(3.6 签名和重载< /a>) 关于函数重载。

函数重载

在这种方法中,您可以有两个或多个具有相同名称的函数。但是
每个函数必须有不同的
签名(即不同类型的
参数、参数序列或
参数数量)。

注意:返回类型不是参数的签名

在您的代码中,您也实现了具有相同名称和签名的两个函数。

You have to read this(3.6 Signatures and overloading) about function overloading.

Function overloading

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

Note: return type is not a signature of parameter

In your code you have implemented both functions with same name and signatures as well.

风吹雪碎 2024-10-19 07:02:18

对于另一个不太优雅的解决方案,想象一个具有“类似向导”的页面结构(视图)的站点,您希望将 ViewModel 从页面 1 传递到页面 2,从页面 2 传递到页面 3 等。

问题是Page 2 的“GET”版本需要从 Page 1 接收模型,但在进行回发时还需要将模型传递给 Page 3。因此,任何“中间”页面的 GET 和 POST 版本都需要包含模型的签名。

解决方法是简单地向签名添加一个“垃圾参数”,通过使用 ? 确保它可以为空。

    [HttpGet]
    public ActionResult Page2(MyModel myModel)
    {
    }

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

For another, less elegant solution, imagine a site with a "Wizard-like" structure of pages (Views), where you want to pass the ViewModel from Page 1 to Page 2, from Page 2 to Page 3 etc.

The problem is that the "GET" version of Page 2 needs to receive the model from Page 1, but also needs to pass the model to Page 3 when doing a postback. Therefore both the GET and POST versions of any 'middle' pages need signature which contains the model.

A workaround is to simply add a "junk parameter" to the signature, making sure that it is nullable by using the ?.

    [HttpGet]
    public ActionResult Page2(MyModel myModel)
    {
    }

    [HttpPost]
    public ActionResult Page2(MyModel myModel, int? i)
    {
    }
初懵 2024-10-19 07:02:18

这是因为您将相同的参数传递给两个函数,尽管您在其中一个函数上指定了 HttpPost,但这是不允许的。您可以更改 Edit Post 函数的名称并在 Html.BeginForm() 中指定它或将参数更改为 FormCollection 而不是 int

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

你另情深 2024-10-19 07:02:18

我认为最简单的方法是向 global.asax.cs 文件添加一个额外的可选参数:

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

并将第二个函数从 更改为

[HttpPost]
public ActionResult Edit(int id)

这样

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

你就不会必须改变你的任何逻辑。因为第二个参数是可选的。如果您不提供该值,它不会抱怨。

I think the easiest way to do this is 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 your second function from

[HttpPost]
public ActionResult Edit(int id)

to

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

This way you don't have to change any of your logic. As the second parameter is optional. It wont complain if you don't provide the value.

刘备忘录 2024-10-19 07:02:18

如果您在 POST 控制器方法上使用视图模型,请确保您的模型有一个空的构造函数。这让我抓狂。

namespace app.Models
{
    public class UserEdit
    {
        public User User { get; set; }

        public UserEdit() { }
    }
}

If you are using a View Model on your POST controller method, make sure your model has an empty constructor. This was driving me nuts.

namespace app.Models
{
    public class UserEdit
    {
        public User User { get; set; }

        public UserEdit() { }
    }
}
柒七 2024-10-19 07:02:18

你可以试试这个。

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

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

You can try this instead.

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

    [HttpPost]
    [ActionName("Edit")]
    public ActionResult EditPosted()
    {
        return View();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文