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

  • SEARCH
  • Home
  • 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 3353768
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:11:29+00:00 2026-05-18T02:11:29+00:00

I have a pretty simple MVC 2 form. It has two dropdowns, user and

  • 0

I have a pretty simple MVC 2 form. It has two dropdowns, user and role. The employee dropdown passes validation, and the role dropdown does not, regardless of what I select. There is no default “empty” option although I plan to implement one, which is why I need the validation to work. It fails both client and server validation. I just can’t see why one would work and one does not!

The Form:

<% using (Html.BeginForm()) {%>

    <%:Html.ValidationSummary(true) %>
    <%:Html.EditorFor(model => model.User, new { AllEmployees = Model.AllEmployees, RoleList = Model.RoleList })%>

    <p>
        <input type="submit" value="Add New User" />
    </p>

    <% } %>

<% Html.EndForm(); %>

The Editor Template:

<tr>
    <td>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.UserId) %>
            <%: Html.RequiredMarkFor(model => model.UserId) %>
        </div>   
    </td>    
    <td>    
    <div class="editor-field">
        <%: Html.DropDownListFor(model => model.UserId, new SelectList(ViewData["AllEmployees"] as IEnumerable, "UserId", "DisplayName", Model.UserId)) %>
        <%: Html.ValidationMessageFor(model => model.UserId>
    </div>    
    </td>       
</tr>     

<tr>
    <td>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.AccessLevel)%>
            <%: Html.RequiredMarkFor(model => model.AccessLevel)%>
        </div>   
    </td>    
    <td>    
        <div class="editor-field">
            <%: Html.DropDownListFor(model => model.AccessLevel, new SelectList(ViewData["RoleList"] as IEnumerable, Model.AccessLevel))%>
            <%: Html.ValidationMessageFor(model => model.AccessLevel)%>
        </div>    
    </td>       
</tr> 

The Metadata:

    [DisplayName("Employee")]
    [Required(ErrorMessage = "Please select an employee.")]
    [StringLength(8, ErrorMessage = "User Id must be less than 8 characters.")]
    [DisplayFormat(ConvertEmptyStringToNull = false,
                            HtmlEncode = true)]
    [DataType(DataType.Text)]
    public object UserId { get; set; }


    // Validation rules for Access Level
    [DisplayName("Role")]
    [Required(ErrorMessage = "Please select the role for this user.")]
    [StringLength(15, ErrorMessage = "Role must be under 15 characters.")]
    [DisplayFormat(ConvertEmptyStringToNull = false,
                            HtmlEncode = true)]
    [DataType(DataType.Text)]
    public object AccessLevel { get; set; }

The Get Action:

    List<String> roles = (from o in txDB.Users
                                      select o.AccessLevel).Distinct().ToList(); 

    var viewModel = new UserViewModel
    {
        User = new User(),
        AllEmployees = empList,
        RoleList = roles
    };
    return View(viewModel);

The Post Action:

    [HttpPost]
    [AuthorizeAttribute(Roles="Administrator")]
    public ActionResult Create(User user)
    {
        if(!ModelState.IsValid)
        {
            //ModelState is invalid
            return View(new User());
        }
        try
        {
           //do stuff
        }
    }

The Required Helper Method (from Define markup for [Required] fields in View in ASP.NET MVC 2.0):

    public static string RequiredMarkFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
    {
        if(ModelMetadata.FromLambdaExpression(expression, helper.ViewData).IsRequired)
            return "*";
        else
            return string.Empty;
    } 
  • 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-05-18T02:11:30+00:00Added an answer on May 18, 2026 at 2:11 am

    Post method should be as follows to get Server side validation…

    [HttpPost]
    [AuthorizeAttribute(Roles="Administrator")]
    public ActionResult Create(User user)
    {
        if(!TryUpdateModel(user))
        {
           // Model is INVALID
           return View(user);
        }
        else
        {
            // ModelState is VALID
            // Do stuff
        }
    }
    

    The else might be redundant depending on what you’re doing but that should get you going.
    In the view above your <% using Html.BeginForm() %> you need

    <% Html.EnableClientValidation(); %>
    

    You also need to reference the scripts, MicrosoftAjax and MicrosoftMvcValidation I think

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

Sidebar

Related Questions

Pretty simple question: When i have a persistable object, it usually has a property
I have a pretty simple ASP.NET MVC page and am using TinyMCE to allow
I have a pretty simple HTML form where users can enter in information about
Should be pretty simple: I have an InputStream where I want to peek at
Pretty simple scenario. I have a web service that receives a byte array that
I am pretty new to php, but I am learning! I have a simple
I have a pretty basic windows form app in .Net. All the code is
In my Asp.net MVC app, I have two methods on a controller, one for
This one should be pretty simple. I am making a non-MVC ASP.NET 2.0 site.
I'm hoping this is a rather simple question, but I'm pretty new to MVC

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.