asp.net mvc 3 jquery 或 ajax 与 @Ajax.BeginForm() 的问题

发布于 2024-12-12 12:05:56 字数 1004 浏览 4 评论 0原文

我正在开发一个简单的 CMS 项目,但我遇到了困难。然后几个小时后,把它炸毁了。

由于某种原因,我在 @Ajax.BeginForm 中的按钮没有激活回发。既不是完整的回发也不是ajax。我引用了脚本,并尝试调试 jQery,检查了控制器、视图、引用和模型中的每个字符,但均无济于事。 没有引发任何错误或异常。

控制器操作:

[HttpPost]
public ActionResult Delete(int ID,int page=1)
{
  //some code...
  //...work, work, work...
}

查看段:

@using (Ajax.BeginForm("Delete", "Client", new { page = Model.CurrentPageIndex }, ao))
{
    @Html.Hidden("clientID", item.ClientID)
    <input type="submit" value="Briši" name="brisi" />
}

错误:

<块引用>

参数字典包含“Info3CRM.WebUI.Controllers”中方法“System.Web.Mvc.ActionResult Delete(Int32, Int32)”的不可空类型“System.Int32”的参数“ID”的空条目。客户端控制器'。可选参数必须是引用类型、可为 null 的类型,或者声明为可选参数。 参数名称:参数

最后我决定禁用 jQuery 和 Ajax 并进行正常的回发。然后抛出异常。我试图在没有所有必要参数的情况下调用控制器中的操作。

我的问题是:如果启用了 jQuery 和 ajax,如何捕获这样的异常?

I am working on a simple CMS project, and i have run into a wall. And then after a lot of hours, blew it up.

For some reason my buttons inside @Ajax.BeginForm were not activating the postback. Neither full postback nor ajax. I have referenced the scripts, and tried to debug jQery, examined every character in the Controllers, Views, References and Models to no avail.
There were no errors or exceptions thrown.

Controller action:

[HttpPost]
public ActionResult Delete(int ID,int page=1)
{
  //some code...
  //...work, work, work...
}

View segment:

@using (Ajax.BeginForm("Delete", "Client", new { page = Model.CurrentPageIndex }, ao))
{
    @Html.Hidden("clientID", item.ClientID)
    <input type="submit" value="Briši" name="brisi" />
}

Error:

The parameters dictionary contains a null entry for parameter 'ID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Delete(Int32, Int32)' in 'Info3CRM.WebUI.Controllers.ClientController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

Finally i have decided to disable jQuery and Ajax and do normal postback. Then the exception was thrown. I was trying to call the Action in controller without all the necessary parameters.

My question to all is: How to catch such an exception if jQuery and ajax are enabled?

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

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

发布评论

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

评论(2

薄情伤 2024-12-19 12:05:56

您可以在某种客户端调试工具中看到此错误。

例如, Fiddler 会向您显示从服务器返回的响应 - 您可以使用 Fiddler 的 WebView 选项卡请参阅 HTML 响应。在这种情况下,HTML 响应将向您显示 ASP.NET 黄色错误屏幕。

除了 Fiddler 之外,您还可以使用 IE、Chrome 或 Opera 中内置的浏览器调试工具 - 或下载 Firebug对于火狐浏览器。每个工具都有一个网络选项卡,它将显示所有 ajax 请求及其响应。

You could see this error in some kind of client side debugging tool.

For example, Fiddler would show you the response that came back from the server - and you can use Fiddler's WebView tab to see the HTML response. The HTML response in this case would show you the ASP.NET yellow error screen.

In addition to Fiddler, you could also use browser debugging tools that come built into IE,, Chrome or Opera - or download Firebug for FireFox. Each tool has a network tab which will show you all the ajax requests and their responses.

厌味 2024-12-19 12:05:56

您的问题是,您要发布的操作期望找到名为 ID 的参数,但没有发布此类参数。如果您更改隐藏输入的名称以匹配参数的名称,那么它应该可以正常工作。

所以,

@Html.Hidden("clientID", item.ClientID)

改为

@Html.Hidden("ID", item.ClientID)

Your problem is that the action you are POSTing to expects to find a parameter named ID, while there is no such parameter being POSTed. If you change the name of the hidden input to match the name of the parameter it should all work fine.

So, change

@Html.Hidden("clientID", item.ClientID)

to

@Html.Hidden("ID", item.ClientID)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文