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 6591551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:26:15+00:00 2026-05-25T17:26:15+00:00

I have a mini form that consists of all select boxes and checkbox lists.

  • 0

I have a mini form that consists of all select boxes and checkbox lists.

I have the data annotations in the viewmodel already set up. So if i submit the form (without javascript enabled), ModelState.IsValid works like it’s supposed to.

But in the client validation, I’m having issues. I have unobtrusive jquery validation on, but the when i do $("#form").valid(), it always returns true.

I’m not sure how to customize this and check for conditions. E.g., i have a checkboxlist which by default has nothing checked. So if nothing is checked, .valid() should return false. Also, 2 of the dropdowns have a ‘please select’ option as their first, but jquery still returns valid. Server side ModelState.IsValid works for both of these.

The following is an example:

@using (Html.BeginForm("index", "home", FormMethod.Post, new { @id = "miniForm" })) {
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()

<div>
    @Html.LabelFor(m => m.NinjaType)
    @Html.DropDownListFor(m => m.NinjaType, Model.NinjaTypeList) // First value is '0'. Rest of the list is of type STRING
</div>

/// following is the rendered html code as i created a helper for it which i've omitted in this example
<ul>
    <li><input type="checkbox" id="poo1" name="pie" value="one" />one</li>
    <li><input type="checkbox" id="poo2" name="pie" value="two" />two</li>
    <li><input type="checkbox" id="poo3" name="pie" value="three" />three</li>
</ul>

<input type="submit" id="submitButton" />
}

If it helps, this is the POST Controller action:

public ActionResult Index(SuperDooperNinjaViewModel m)
{
    if (this.ModelState.IsValid)
        return Redirect("win");
    else 
        return Redirect("fail");
}

I was thinking that i’d do it the old fashioned way on $('#submitButton').click();, but I have a feeling there might be a better way to do this. Another way I’ve thought of, is turn it into an Ajax form. So in the server side response, instead of Redirect("fail");, i return a JsonResult.

So in summary, what would the best route be here to validating this form?

  • NinjaType’s value must not be ‘0’
  • At least one check box must be checked.

Thanks in advance

UPDATE:

ViewModel:

public class SuperDooperViewModel
{
    [Required]
    public string NinjaType {get;set;}
    public IEnumerable<SelectItemList> { get;set; }

    [Required]
    public string[] pie {get;set;} // checkbox
    public IEnumerable<string> PieList { get;set; } // list of values for checkbox
}

In jquery, i’m testing it like this:

$("#submitButton").click(function(e) {
    e.preventDefault();
    if ($("#miniForm").valid())
         alert("valid");
    else
         alert("fail");
});
  • 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-25T17:26:15+00:00Added an answer on May 25, 2026 at 5:26 pm

    I’m not sure if this is the best way to do it, but for now, i’ve thrown the checkboxlist extension out, and just manually done this:

    @foreach(var nt in Model.NinjaTypeList)
    {
        <li>@Html.CheckBox("pie", new { @id = nt })</li>
    }
    @Html.HiddenFor(m => m.NinjaType)
    

    In jquery, i’ve attached an event to the checkboxes:

    $('input[name=pie]').click(function(){
        var pie = "";
        $('input[name=pie]').each(function(i) {
           if ($(this).is(':checked'))
               pie += $(this).val() + ';';
        });
        $("#NinjaType").val(pie);
    });
    

    In my controller:

    public ActionResult Index(SuperDooperViewModel m, string[] pie)
    {
        /// I've add the following as a work around:
        if (string.IsNullOrEmpty(m.NinjaType) // no reason it should be, but it's a sort of failsafe
        {
             if(pie.Any())
                  m.NinjaType = string.Join(";", pie.Where(w => w != "false"));
        }
    
        // rest of the controller here
    }
    

    This basically does what I want, but it seems like a bit of a work around to me. Any improvements would be appreciated.

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

Sidebar

Related Questions

I have a form that once you select from a drop-down list and select
Sorry all, I do have this mini table with 3 columns but, I will
I have made a SVG image, or more like mini application, for viewing graphs
i have a mini app where users can login, view their profile, and follow
I have a plist file that is brought into a tableview, and now want
I have a strange build problem. I have a simple test program that sends
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I

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.