ASP.NET MVC PartialView 回发:如何验证数据?

发布于 2024-09-17 18:31:36 字数 1384 浏览 7 评论 0原文

我正在使用 ASP.NET 部分视图,就像在本例中一样,

<% using (Html.BeginForm()) { %>
    <table cellspacing="2" cellpadding="0" border="0" width="100%">
    <tr>
        <td><%= Html.LabelFor(model => model.PersonName)%></td>
        <td>
            <%= Html.TextBoxFor(model => model.PersonName)%>
            <%= Html.ValidationMessageFor(model => model.PersonName, "*")%>
        </td>
    </tr>
    ...
    <tr><td colspan="2"><%= Html.ValidationSummary(false) %></td></tr>
    </table>
<% } %>

我在 Jquery 对话框中显示这些部分视图,使用 jquery 代码调用它们

$.ajax({
    type: "get",
    dataType: "html",
    url: urlAction,
    data: {},
    success: function(response) {
        $("#panelDetail").html('').html(response).dialog('open');
    }
});

,一切正常,让我很高兴。我还可以使用 jquery ajax 提交表单,这让我更加高兴。 :)

真正令人烦恼的是,我不明白验证发生在哪里,因为当验证发生时,它会完全刷新页面并关闭对话框。

我确信有人可以提供帮助。无论如何……谁会是? :)

提前致谢!

编辑

这是控制器操作签名,其中包含一些代码

[HttpPost]
public ActionResult MyAction(FormCollection form) {
    string foroID = form["ForoId"];
    string foro = form["Foro"];
    string authorityId = form["AuthorityId"];
    string sezione = form["Sezione"];
    ...
}

我是否必须重新创建模型类来验证它?

I am using ASP.NET partial views like in this example

<% using (Html.BeginForm()) { %>
    <table cellspacing="2" cellpadding="0" border="0" width="100%">
    <tr>
        <td><%= Html.LabelFor(model => model.PersonName)%></td>
        <td>
            <%= Html.TextBoxFor(model => model.PersonName)%>
            <%= Html.ValidationMessageFor(model => model.PersonName, "*")%>
        </td>
    </tr>
    ...
    <tr><td colspan="2"><%= Html.ValidationSummary(false) %></td></tr>
    </table>
<% } %>

I show these partial views in Jquery dialogs calling them using jquery code

$.ajax({
    type: "get",
    dataType: "html",
    url: urlAction,
    data: {},
    success: function(response) {
        $("#panelDetail").html('').html(response).dialog('open');
    }
});

and everything works and make me happy. I am also able to submit the form using jquery ajax and this make me even more happy. :)

What is really annoying is that I did not understand where validation occurs because, when it happens, it does a full refresh of the page and close the dialog.

I am sure that somebody can help on this. Anyway....who will be? :)

Thanks in advance!

EDIT:

This is the controller action signature with some code in it

[HttpPost]
public ActionResult MyAction(FormCollection form) {
    string foroID = form["ForoId"];
    string foro = form["Foro"];
    string authorityId = form["AuthorityId"];
    string sezione = form["Sezione"];
    ...
}

Do I have to re-create the model class to validate it?

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

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

发布评论

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

评论(2

欢你一世 2024-09-24 18:31:36

首先:如果您使用客户端验证,则第一个检查已经在客户端使用 JavaScript 进行,但您的验证在输入数据时会显示错误。

第二点:只要您的操作采用与 PartialView 相同类型的参数,您发送回控制器操作的数据就会在该特定点进行验证模型类型。如果此类型(类)具有附加到属性的数据注释,则这些数据注释正在被验证。当然,您也应该检查您的操作中是否存在模型错误并采取相应的行动。

重定向?如果您提供更多有关控制器操作的代码,我们可以更轻松地检查发生的情况并提供一些额外的帮助。

我如何处理这种情况

我创建了一个特殊的操作过滤器来检查模型状态错误并向客户端返回 400 以及错误描述。我的所有 Ajax 调用都会处理成功以及错误回复并采取相应措施。

检查此处的代码(问题和解决方案)

实际解决方案

此解决方案基于从下面的评论中获得的附加信息

因此,经过讨论,问题实际上不是整页回发,而是根本没有发生的验证。原因是控制器操作采用 FormCollection 类型的参数。 MVC 框架不知道如何验证该数据,因此不会发生验证。

您要做的更改是更改此参数的类型,以匹配您的强类型视图。如果您的视图类型为 ViewPage,那么您的 HttpPost 操作很可能应该具有相同类型的参数。

一般来说,动作类型可以具有任何类型的参数。是的,它可以有更多的参数。唯一的限制是默认模型绑定器将能够将发布的数据与这些参数相关联。这就是您的用武之地。您所要做的就是正确命名参数,模型绑定器将完成剩下的工作。

如果您遇到无法轻松完成此操作的特定情况,您始终可以为特定类型编写自定义模型绑定程序,并且如何解析发布的数据将完全取决于您。

First of all: if you're using client validation, than the first check is already on the client side using JavaScript, but your validation would show errors while typing in data.

Second of all: your data being sent back to controller action is being validated at that particular point as long as your action takes a parameter of the same type as your PartialView has as model type. If this type (class) has data annotations attached to properties, those are being validated. Of course you should as well check for model errors in your action and act accordingly.

A redirect? If you'd provide some more code of your controller action we could more easily check what's going on and provide some additional help.

How did I handle this situation

I created a special action filter that checks for model state errors and returns 400 to the client with error description. All my Ajax calls handle success as well as error replies and act accordingly.

Check the code here (question and solution).

The actual solution

This solution is based on additional information gotten from comments below

So after a discussion the problem isn't actually the full page post-back, but the validation that didn't happen at all. The reason being that controller action takes a parameter of type FormCollection. MVC framework has no knowledge whatsoever to know how to validate that data, so no validation happens.

The change you have to do is to change the type of this parameter, to match your strong type view. If your view is of type ViewPage<MyCustomType> then your HttpPost action should most probably have a parameter of the same type.

In general action type can have parameters of any type. Yes it can have even more parameters. The only restriction being that default model binder will be able to relate posted data to these parameters. This is where you come in. All you have to do is to name your parameters properly and model binder will do the rest.

If you come across a certain situation where this can't be done easily, you can always write a custom model binder for a particular type, and it will be completely on you how to parse posted data.

夏雨凉 2024-09-24 18:31:36

根据罗伯特在此处的建议,

在关闭对话框之前,您尝试在通过ajax提交表单时检查服务器响应是什么在关闭对话框之前,请在调用对话框的 dialog('close') 之前尝试检查响应是有效还是无效的模型状态。在所有其他对话框情况下也是如此。

with Robert's suggestion in here

before you close your dialog box, you try to check what is the server response when you submit the form via ajax before you close the dialog, try to check if the response is valid or invalid model state before you call dialog('close') of the dialog. Same thing in all other situation of dialogs.

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