简体   繁体   English

服务器端/客户端验证.net

[英]Server side/client side validation .net

Ok, so I have a kind of weird problem that I need ideas on how to solve. 好的,所以我有一个奇怪的问题,我需要有关如何解决的想法。

I have a vb.net web application that points to a sql database. 我有一个指向sql数据库的vb.net Web应用程序。 There's a table with a primary key that is an auto-incremented integer. 有一个带有主键的表,该主键是一个自动递增的整数。

When a user adds an object to this table, it doesn't currently check to see if the "First Name" and "Last Name" already exist in one of the datarows. 用户将对象添加到此表时,当前不会检查其中一个数据行中是否已存在“名字”和“姓氏”。 The desired addition to the functionality is as follows: 所需的功能添加如下:

1)When the user submits the form, check the table to see if such a record already exists. 1)当用户提交表单时,检查表以查看是否已经存在这样的记录。

1.1)If the record doesn't exist proceed with the insert. 1.1)如果记录不存在,请继续插入。

2)If that record does exist, display a warning to the user that such a record exists 2)如果该记录确实存在,则向用户显示该记录存在的警告

2.1)The warning should have two buttons, "Continue" and "Cancel" 2.1)警告中应有两个按钮,“继续”和“取消”

2.1.1)If the user clicks "Continue" go ahead and add the duplicate record 2.1.1)如果用户单击“继续”,请添加重复的记录

2.1.2)If the user clicks "Cancel" stop the insert. 2.1.2)如果用户单击“取消”,则停止插入。

I'm still relatively new to web development (a little over a year of experience). 我还是Web开发的新手(一年多的经验)。 I am looking for the "correct" way to do this. 我正在寻找“正确”的方式来做到这一点。 The aspect of this task that is making it hard for me is that I have to run the query, and then possibly display and alert (javascript probably). 该任务使我感到困难的方面是,我必须运行查询,然后可能显示并发出警报(可能是JavaScript)。 I'm not sure how to display an alert in the middle of the server side validation. 我不确定如何在服务器端验证的中间显示警报。

Any ideas or comments are appreciated! 任何想法或意见表示赞赏!

Thanks! 谢谢!

If you wouldn't allow insertion of duplicates, you could just create unique index in your database. 如果您不允许插入重复项,则可以在数据库中创建唯一索引。 However, what you can do now is to get the count of records in the database, where firstname and lastname equals to inserted. 但是,您现在可以做的是获取数据库中记录的计数,其中名和姓等于插入。

In case of normal SQL it would look like SELECT COUNT(recordID) WHERE firstName = @firstName AND lastName = @lastName; 在普通SQL的情况下,它看起来像是SELECT COUNT(recordID),其中firstName = @firstName AND lastName = @lastName;

Or it could look even easier with Entity Framework. 或者,使用实体框架看起来会更加容易。 Anyway, your question was about "displaying alert in the middle of server side validation". 无论如何,您的问题是关于“在服务器端验证过程中显示警报”。 Think about it differently. 换个角度思考。 Think about it as about two checks. 将其视为两次检查。

Add another control to your input form, an invisible checkbox near the Submit button. 在输入表单中添加另一个控件,即“提交”按钮附近的不可见复选框。 It should contain the expression about user's agreement to insert duplicate record. 它应包含有关用户同意的语句,以插入重复记录。 Once you detect, that record is duplicate, interrupt the validation, and make checkbox visible, but Submit button - disabled. 一旦检测到,该记录就是重复的,请中断验证并使复选框可见,但“提交”按钮将被禁用。 When user checks the checkbox, Submit button should become visible again. 当用户选中复选框时,“提交”按钮应再次变为可见。

Now, since you are going through the same validation again, you have to take your checkbox into equation - if it is visible and checked, you don't have to check for record duplication anymore, and just submit the record. 现在,由于您要再次进行相同的验证,因此您必须将复选框纳入等式-如果它是可见的并已选中,则不必再检查记录重复,只需提交记录即可。 If you need to re-use that input form, don't forget to uncheck the checkbox and make it invisible once again. 如果您需要重新使用该输入表单,请不要忘记取消选中该复选框并使其再次不可见。

What you want to do here is add a confirm parameter or something like that to your method, like this: 您要在此处执行的操作是在您的方法中添加一个confirm参数或类似的东西,如下所示:

' This is just pseudocode; I'm guessing you can translate it to
' whatever web framework you're using
Sub InsertRecord(ByVal name() As String, Optional ByVal confirm As Boolean = False)
  If DuplicateRecord(name) And Not confirm
    ' Here's where you would render the page with a confirmation dialog
    RenderViewToRequestConfirmation()
    Return
  End

  DoInsertRecord(name)
  RenderViewAfterRecordInserted()
End

Then under normal circumstances, from your front end you would submit a request that would call this method without the confirm parameter. 然后,在正常情况下,您将从前端提交一个请求,该请求将调用不带confirm参数的此方法。 In the case of duplicate records, the server would render a response with a dialog requesting confirmation. 在记录重复的情况下,服务器将通过对话框请求确认来呈现响应。 If the user clicked 'Yes' (or whatever), then the front end would send the same request but this time with the necessary request params to set confirm to True . 如果用户单击“是”(或其他),则前端将发送相同的请求,但这一次带有必要的请求参数以将confirm设置为True

In terms of web requests, this process might look like: 就Web请求而言,此过程可能类似于:

| Request Data                             | Response               |
|------------------------------------------|------------------------|
| { "name": "M Webster" }                  | Page w/ confirm dialog |
| { "name": "M Webster", "confirm": true } | Success page           |

The route that I went is a little confusing... even now that I have it working, but i'll explain it here in case it makes sense to someone else who can better explain it later. 我走的路线有点令人困惑……即使现在我可以使用它了,但我还是会在这里进行解释,以防其他人可以在以后更好地解释它。

I wrote a function in the code behind that calls the function that checks the database for a duplicate record, but I added these two lines of code above the function declaration: 我在后面的代码中编写了一个函数,该函数调用该函数检查数据库是否存在重复记录,但是我在函数声明上方添加了这两行代码:

<System.Web.Services.WebMethod()>
<System.Web.Script.Services.ScriptMethod()>

Then, I wrote a JavaScript function that grabs the value from the Textbox and passes the value to that function in the code behind. 然后,我编写了一个JavaScript函数,该函数从Textbox中获取值,并将该值通过后面的代码传递给该函数。 This is made possible by those two lines above the function declaration in the code behind and by using the PageMethods object. 这可以通过后面代码中函数声明上方的两行以及使用PageMethods对象来实现。 The actual call from the JavaScript would look like this: 来自JavaScript的实际调用如下所示:

PageMethods.FunctionName(parameter, function(returnValueFromOtherFunction){ 
    ....function stuff
});

Then I assigned the JavaScript function to the onBlur event in the Textbox. 然后,我将JavaScript函数分配给Textbox中的onBlur事件。

Thanks for the help guys, but I think this is the best way to solve my problem. 感谢您的帮助,但是我认为这是解决我的问题的最佳方法。

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

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