ViewResult 和 ActionResult 包含相同参数时出错
在我的控制器中,我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以实现视图模型,以便 EditViewModel 包含您希望用户能够编辑的所有字段,并在 Edit GET 方法中返回这些字段,并为视图模型提供强类型视图。那么这意味着在您的 POST 方法中您将传递 EditViewModel 作为参数,有点像这样:
因此您的 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:
And so your GET and POST methods would have different signatures. Hope this helps.
您必须阅读此内容(3.6 签名和重载< /a>) 关于函数重载。
函数重载
在您的代码中,您也实现了具有相同名称和签名的两个函数。
You have to read this(3.6 Signatures and overloading) about function overloading.
Function overloading
In your code you have implemented both functions with same name and signatures as well.
对于另一个不太优雅的解决方案,想象一个具有“类似向导”的页面结构(视图)的站点,您希望将 ViewModel 从页面 1 传递到页面 2,从页面 2 传递到页面 3 等。
问题是Page 2 的“GET”版本需要从 Page 1 接收模型,但在进行回发时还需要将模型传递给 Page 3。因此,任何“中间”页面的 GET 和 POST 版本都需要包含模型的签名。
解决方法是简单地向签名添加一个“垃圾参数”,通过使用 ? 确保它可以为空。
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 ?.
这是因为您将相同的参数传递给两个函数,尽管您在其中一个函数上指定了 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
我认为最简单的方法是向
global.asax.cs
文件添加一个额外的可选参数:并将第二个函数从 更改为
这样
你就不会必须改变你的任何逻辑。因为第二个参数是可选的。如果您不提供该值,它不会抱怨。
I think the easiest way to do this is add an additional optional parameter to the
global.asax.cs
file:and change your second function from
to
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.
如果您在 POST 控制器方法上使用视图模型,请确保您的模型有一个空的构造函数。这让我抓狂。
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.
你可以试试这个。
You can try this instead.