简体   繁体   English

使用xval的C#mvc2客户端表单验证,防止发布

[英]C# mvc2 client side form validation with xval, prevent post

I'm using xval to use client side validation in my asp.net mvc2 web-application. 我正在使用xval在asp.net mvc2 Web应用程序中使用客户端验证。 Despite the errors it's giving when I enter text in a numeric field, it still tries to post the form to the database. 尽管我在数字字段中输入文本时出现错误,但它仍然尝试将表单发布到数据库中。 The incorrect values are being replaced by 0 and saved to the database. 不正确的值将替换为0并保存到数据库。 But instead it shouldn't even be possible to try and submit the form. 但是相反,甚至不可能尝试提交表单。 Can anyone help me out here? 有人可以帮我从这里出去吗?

I've set the attributes as below: 我将属性设置如下:

[Property]
[ShowColumnInCrud(true, label = "FromPriceInCents")]
[Required]
//[Range(1, Int32.MaxValue)]
public virtual Int32 FromPriceInCents{ get; set; }

The controller catching the request looks as below; 捕获请求的控制器如下所示; I'm getting no errors in this part. 我在这部分没有任何错误。

[AcceptVerbs(HttpVerbs.Post)]
[Transaction]
[ValidateInput(false)]
public override ActionResult Create()
{
  //some foo happens
}

My view looks like below: 我的看法如下所示:

<div class="label"><label for="Price">FromPrice</label></div>
<div class="field">
<%= Html.TextBox("FromPriceInCents")%>
<%= Html.ValidationMessage("product.FromPriceInCents")%></div>

And at the end of the view i have the following rule which in html code generates the correct validation rules 在视图的最后,我有以下规则,该规则在html代码中生成正确的验证规则

<%= Html.ClientSideValidation<Product>("Product") %>

I hope someone can helps me out with this issue, thanks in advance! 我希望有人能帮助我解决这个问题,谢谢!

EDIT: 19th April I just found out that there is a normal button with being used instead of an input type="Button" Could this be the issue? 编辑:4月19日,我刚刚发现有一个正常的按钮正在使用,而不是输入type =“ Button”可能是问题吗?

<button class="save" type="submit" name="save"><span>Opslaan</span></button>

My first and main concern here would be: why is your app saving the values in the database in the first place? 我首先要关注的是:为什么您的应用程序首先将值保存在数据库中? While xVal is a good way to make the application user friendly, you still HAVE to do server side validation. 尽管xVal是使应用程序易于使用的好方法,但您仍然必须进行服务器端验证。 If you don't validate your data on the server - you have a masive security hole! 如果您不验证服务器上的数据,则将面临巨大的安全漏洞! Try checking if the ModelState.IsValid in your controller before saving the values. 保存值之前,请尝试检查控制器中是否有ModelState.IsValid。

Now, from what I see you're registering the xVal validation using 现在,据我所知,您正在使用来注册xVal验证

<%= Html.ClientSideValidation<Product>("Product") %>

The way it works is it enables client side validation for all controls prefixed with "Product". 它的工作方式是为所有以“ Product”为前缀的控件启用客户端验证。 Your textbox on the other hand has an id of FromPriceInCents 另一方面,您的文本框的ID为FromPriceInCents

So the solution here would be to do this: 因此,这里的解决方案是这样做:

<%= Html.TextBox("FromPriceInCents")%>
<%= Html.ValidationMessage("FromPriceInCents")%>

<%= Html.ClientSideValidation<Product>() %>

UPD3 I updated the post. UPD3我更新了帖子。 Fixed the code so that the prefix is not used. 修复了代码,因此不使用前缀。

Also, I compiled a working solution that contains a working solution. 另外,我编译了一个包含工作解决方案的工作解决方案。 List, Edit, Create page, string and int properties, and xVal validation. 列出,编辑,创建页面,字符串和int属性以及xVal验证。

public class Product
{
    [ScaffoldColumn(false)]
    public int Id { get; set; }

    [Required]
    [Range(1,50)]
    public int PriceInCents { get; set; }

    [Required]
    [StringLength(50)]
    public string Name { get; set; }
}

and on the view 并在视图上

<%= Html.TextBoxFor(model => model.PriceInCents) %>
<%= Html.ValidationMessageFor(model => model.PriceInCents) %>

Here's the download link.. Check it out and tell me if it works http://www.flexlabs.org/download/xValTest 这是下载链接。请检查一下并告诉我它是否有效http://www.flexlabs.org/download/xValTest

Why do you have the Range attribute commented out? 为什么将Range属性注释掉了? With the properties you have specified as long as anything is entered in the textbox it should pass client side validation. 使用您指定的属性,只要在文本框中输入任何内容,它就应该通过客户端验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM