Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8426437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:31:59+00:00 2026-06-10T04:31:59+00:00

I have a model class, which I am using to create the form Public

  • 0

I have a model class, which I am using to create the form

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

My View class, using the above ViewModel to generate form utilising HTML Helpers

<% 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%>

Now what is the best way to add validation to this case?


Update:

Here is what I have tried in another case, but still I dont see any validation, the action is called and processes.

Here is my model class

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

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

End Class

This is the partial View

<% 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%>

Login Action

<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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:32:00+00:00Added an answer on June 10, 2026 at 4:32 am

    You could use data annotations. For example if the UserName field was required you decorate it with the <Required> attribute:

    <Required>
    Public Property UserName As String
    

    and inside the view you put a corresponding placeholder for the error message:

    <%= Html.TextBoxFor(Function(x) x.UserName) %>
    <%= Html.ValidationMessageFor(Function(x) x.UserName) %>
    

    or if you wanted to centralize all error messages in a single place you could use the ValidationSummary helper:

    <%= Html.ValidationSummary() %>
    

    And inside the POST controller action you could verify if the model is valid using ModelState.IsValid boolean property:

    <HttpPost()>
    Function Create(model As Users) As ActionResult
        If ModelState.IsValid Then
            ' validation succeeded => do some processing and redirect
            Return RedirectToAction("Create")
        End If
    
        ' validation failed => redisplay the same view so that the user
        ' can fix the errors
        Return View(model)
    End Function
    

    And here’s an article on MSDN which explains this as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an MVC2 app using DataAnnotations. I have a following Model: public class
in my zf application i have base model class Application_Model, which connects to db
I have a simple model class (Part), which pulls from it's information from a
I have a Query model: class Query < ActiveRecord::Base belongs_to :test end which is
I have a model in my rails application which is class Person < ActiveRecord::Base
i have a class User which extends Ebean Model. And i defined a dbfile
I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I have a model which has a certain behaviour implemented. class X { ....
I have a model called Contact which has_one guest like so. class Contact <
I have a model containing ImageField which should be resized after uploading. class SomeModel(models.Model):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.