0

フォームの作成に使用しているモデルクラスがあります

Public Class Users
    Public Property Id As Integer
    Public Property UserName As String
    Public Property UserNin As Int16
    Public Property UserType As String
    Public Property Password As String

    Property UserTypes As IEnumerable(Of SelectListItem) = {
            New SelectListItem With {.Value = "admin", .Text = "Admin"},
            New SelectListItem With {.Value = "doctor", .Text = "Doctor"},
            New SelectListItem With {.Value = "reception", .Text = "Receptionist"}
    }
End Class

上記のViewModelを使用してHTMLヘルパーを使用してフォームを生成するMyViewクラス

<% Using (Html.BeginForm("Create", "User", FormMethod.Post))%>
    <table>
        <tr>
            <td>
                User Name
            </td>
        </tr>
        <tr>
            <td>
                <%= Html.TextBoxFor(Function(x) x.UserName)%>
            </td>
        </tr>
        <tr>
            <td>
                User NIN
            </td>
        </tr>
        <tr>
            <td>
                <%= Html.TextBoxFor(Function(x) x.UserNin)%>
            </td>
        </tr>
        <tr>
            <td>
                Password
            </td>
        </tr>
        <tr>
            <td>
                <%= Html.PasswordFor(Function(x) x.Password)%>
            </td>
        </tr>
        <tr>
            <td>
                <%= Html.DropDownListFor(Function(x) x.UserType, Model.UserTypes)%>
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" value="Add New User" />
            </td>
        </tr>
    </table>
<% End Using%>

では、このケースに検証を追加するための最良の方法は何ですか?


アップデート:

これが別のケースで試したものですが、それでも検証が表示されず、アクションが呼び出されて処理されます。

これが私のモデルクラスです

Public Class LoginUser
    <Required()>
    Public Property UserName As String

    <Required()>
    <StringLength(8)>
    Public Property Password As String

End Class

これは部分的なビューです

<% Using (Html.BeginForm("Login", "User", FormMethod.Post))%>
<% Html.EnableClientValidation()%>
<table ID="loginTable" runat="server">
    <tr>
        <td>
            <label for="username">UserName</label>
        </td>
    </tr>
    <tr>
        <td>
            <%= Html.TextBoxFor(Function(x) x.UserName)%>
            <%= Html.ValidationMessageFor(Function(x) x.UserName) %>
        </td>
    </tr>
    <tr>
        <td>
            <label for="password">Password</label>
        </td>
    </tr>
    <tr>
        <td>
            <%= Html.TextBoxFor(Function(x) x.Password)%>
            <%= Html.ValidationMessageFor(Function(x) x.Password)%>
        </td>
    </tr>
    <tr>
        <td>
            <input type="submit" value="Login" />
        </td>
    </tr>
</table>
<% End Using%>

ログインアクション

<HttpPost()>
Function Login() As ActionResult
    If ModelState.IsValid Then
        Dim sql As String
        Dim username As String = Request.Form("username").ToString
        Dim password As String = Request.Form("password").ToString
        Dim dbHelper As New DBHelper(False)

        sql = "SELECT * FROM " & _tblName & " WHERE username = '" & username & "' and password = '" & password & "'"
        Try
            Dim dr As DataRow = dbHelper.ExecuteAndGetRow(sql)

            If Convert.ToInt16(dr.Item(0).ToString) > 0 Then
                Dim nin As String = dr.Item(4)
                Session("loggedin") = 1
                Session("logged") = dr.Item(0)
                Session("logged_nin") = dr.Item(4)
                ViewData("message") = "Login Successful"
                ViewData("show_loginForm") = False
            Else
                ViewData("message") = "Login failed"
                ViewData("show_loginForm") = True
            End If
        Catch ex As Exception
            ViewData("message") = "Login failed"
            ViewData("show_loginForm") = True
        End Try


        Return View()
    Else
        Return View()
    End If
End Function
4

1 に答える 1

1
于 2012-08-23T08:24:13.317 に答える