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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:06:41+00:00 2026-06-09T13:06:41+00:00

I have the following code: Index.cshtml: @using System.Web.Script.Serialization @model MvcApplication3.ViewModels.PersonViewModel <script src=../../Scripts/jquery.validate.min.js type=text/javascript></script> <script

  • 0

I have the following code:

Index.cshtml:

    @using System.Web.Script.Serialization
@model MvcApplication3.ViewModels.PersonViewModel
<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
<script src="../../Scripts/knockout-2.1.0.js" type="text/javascript"></script>
    <!-- This is a *view* - HTML markup that defines the appearance of your UI -->
<form class="transferListEditor" data-bind="submit: save">

    <p>First name: <input class='required' data-bind="value: firstName, uniqueName: true" /></p>
    <p>Last name: <input class='required' data-bind="value: lastName, uniqueName: true" /></p>

    <table>
        <thead>
            <tr>
                <th>Name</th>
            </tr>
        </thead>
        <tbody data-bind="foreach: activities">
            <tr>
                <td><input data-bind="value: Name" /></td>
                <td><a href="#" data-bind="click: $root.removeActivity">Remove</a></td>
            </tr>    
        </tbody>
    </table>
    <button data-bind="click: addActivity">Add activity</button>
    <button type="submit">Submit</button>
</form>

<script type="text/javascript">

    var initialData = @Html.Raw(new JavaScriptSerializer().Serialize(Model));

    function activity(name) {
        var self = this;
        self.Name = name;
    }

    // This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI
    var viewModel = {
        firstName: ko.observable(initialData.Person.FirstName),
        lastName: ko.observable(initialData.Person.LastName),
        activities: ko.observableArray(initialData.Person.Activities),

        removeActivity : function(activity) {

            viewModel.activities.remove(activity);
        },

        addActivity : function() {
            viewModel.activities.push(new activity(""));
        },

        save: function() {

            $.ajax({
                type: "POST",
                url: "/Home/Index",
                data: ko.toJSON({ Person: viewModel }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    $("#resultCount").html(data);
                }
            });
        }
    };

    // Activates knockout.js
    ko.applyBindings(viewModel);

    $("form").validate({ submitHandler: viewModel.save });
</script>

HomeController:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ahb.Insite.HerdRegistration.WebUI;
using MvcApplication3.Models;
using MvcApplication3.ViewModels;

namespace MvcApplication3.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var person = new Person {FirstName = "John", LastName = "Cool"};

            person.Activities = new List<Activity> { new Activity { Name = "Skiing" } };

            var personViewModel = new PersonViewModel  { Person = person };

            return View(personViewModel);
        }

        [HttpPost]
        public ActionResult Index(PersonViewModel personViewModel)
        {
            //Save it

            return View();
        }
    }
}

So when I click submit I get the required field message appearing for firstname and lastname if they are blank. The problem is even if there is a required field missing the save method on the viewModel still gets called.

Anyone know how to fix?

  • 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-09T13:06:42+00:00Added an answer on June 9, 2026 at 1:06 pm

    Try removing data-bind="submit: save" on the form.

    Since the knockout bindings are being bound before the jQuery validation binding, it could just be hitting the save code first.

    I notice the example of using jQuery validation with knockout doesn’t have a data-bind on the form element.

    Update: I confirmed I could reproduce your problem by adding that data-bind attribute to the example.

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

Sidebar

Related Questions

I have the following code: Index.cshtml: @using System.Web.Script.Serialization @model MvcApplication3.ViewModels.PersonViewModel <script src=../../Scripts/knockout-2.1.0.js type=text/javascript></script> <!--
I have the following code. Index.cshtml: @using System.Web.Script.Serialization @model MvcApplication3.Models.Person <script src=../../Scripts/knockout-2.1.0.js type=text/javascript></script> <!--
I have the following code <xsl:result-document href=output1/output3/index.html format=html> <html> <head> <SCRIPT LANGUAGE=JavaScript> function getParams()
js undefined error we have the following widget code : index.html <script type=text/javascript language=javascript>
I have the following code: Url.Action(Index, Home, new {param1 = Model.Param1, param2 = Model.Param2}
In my Zend Framework app, I have following code: index.php $app_started_time = time(); layout.phtml
I have the following code: index.jsp: <form name=f action=db/ajoutConducteur.jsp method=post> <input type=text pattern=\d{8} required
I have the following code in my index view. latest_entry_list = Entry.objects.filter(is_published=True).order_by('-date_published')[:10] for entry
I have the following code: $.ajax({ type: 'GET', url: 'index.php?route=checkout/onepagecheckout/getpaypaldata', dataType: 'json', success: function(json)
I have the following code: List<int> intList = new ArrayList<int>(); for (int index =

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.