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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:38:33+00:00 2026-06-01T05:38:33+00:00

I am trying to understand how I should validate on the client sections of

  • 0

I am trying to understand how I should validate on the client sections of my MVC3 page independently and have come up with a simplyfied version of what I am trying to achieve.

If I use one form:
Pros: When I submit back to the “PostData” controller method I receive all data contained within the form. In this case both values “name” and “description”, which means that I can instantiate “PersonHobbyModel” and assign the data I have received. I can either store in the database or I can return the same view.

Cons: I cant validate independently. So if “name” isn’t completed and I complete “description” I can still submit the page. (This is a simplyfied version of what I am trying to do and I would have more fields than just “name” and “description”)

With two forms:
Pros: I can validate independently.

Cons: The controller method only receives the subitted forms data which, in this case either “Persons name” or “Hobby description” which means that I can’t recreate a full instance of “PersonHobbyModel”.

This is the model:

 public class Person {

    [Display(Name = "Person name:")]
    [Required(ErrorMessage = "Person name required.")]
    public string Name { get; set; }
}

public class Hobby {

    [Display(Name = "Hobby  description:")]
    [Required(ErrorMessage = "Hobby description required.")]
    public string Description { get; set; }
}

public class PersonHobbyModel {

    public PersonHobbyModel() {
        this.Person = new Person();
        this.Hobby = new Hobby();
    }

    public Person Person { get; set; }
    public Hobby Hobby { get; set; }
}

This is the controller:

 public class PersonHobbyController : Controller
{
    //
    // GET: /PersonHobby/

    public ActionResult Index()
    {
        var model = new PersonHobbyModel();
        return View(model);
    }


    public ActionResult PostData(FormCollection data) {
        var model = new PersonHobbyModel();

        TryUpdateModel(model.Person, "Person");
        TryUpdateModel(model.Hobby,"Hobby");

        return View("Index", model);
    }

}  

This is the view:

@model MultipleFORMStest.PersonHobbyModel
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
@using (Html.BeginForm("PostData", "PersonHobby")) {
    <div>
        @Html.LabelFor(model => model.Person.Name)
        @Html.TextBoxFor(model => model.Person.Name)
        @Html.ValidationMessageFor(model => model.Person.Name)
        <input type="submit" value="Submit person" />
    </div>
}
@using (Html.BeginForm("PostData", "PersonHobby")) {
    <div>
        @Html.LabelFor(model => model.Hobby.Description)
        @Html.TextBoxFor(model => model.Hobby.Description)
        @Html.ValidationMessageFor(model => model.Hobby.Description)
        <input type="submit" value="Submit hobby" />
    </div>
    }

UPDATE 1
I didnt mention, as I wanted to keep the question as simple as possible, but for one of the sections I am using “jquery ui dialog”. I initially used a DIV to define the dialog, which I had inside my main form. This would of caused one problem as I wouldn’t have been able to validate on the client the “JQuery dialog form” independently from the rest of the form.

Saying this jquery did removed the “div jquery ui dialog” from the main form which made me include the dialog in it’s own form. For this reason I have ended up with two forms. The advantage is that I can now independently validate the “jquery dialog ui form”.

But I am confused as to how should I handle on the server data submited from various forms on the client as there is a chance that the user has JS disabled. If I submit from one form I can’t access the data in other forms.

UPDATE 2
Thanks for the replies. I believe I do need two forms and two entities as I want to validate them independently on the client, (apart from being kind of forced to by “Jquery UI Dialog”). For instance if I have, instead of one hobby I have a list of hobbies, which I could posible display in a grid in the same view. So I could not fill in the person name, but continue to add hobbies to the grid, If I do not complete the hobby description I’d get a validation error. (Sorry as I should of included both of my updates in the initial question but for the purpose of clarity I wanted to keep it as simple as posible)

  • 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-01T05:38:35+00:00Added an answer on June 1, 2026 at 5:38 am

    From my perspective, you have a single view model that corresponds to two entity models. In your place I would use a single form and validate the view model and not really think about it as two (dependent) entities. Receive back the view model in your action, instead of a generic form collection, and use model-based validation via data annotation attributes. Once you have a valid, posted model you can then translate that into the appropriate entities and save it to the database.

    Model

    public class PersonHobbyViewModel {
    
        [Display(Name = "Person name:")]
        [Required(ErrorMessage = "Person name required.")]
        public string Name { get; set; }
    
        [Display(Name = "Hobby  description:")]
        [Required(ErrorMessage = "Hobby description required.")]
        public string Description { get; set; }
    }
    

    Controller

    public class PersonHobbyController : Controller
    {
        //
        // GET: /PersonHobby/
        [HttpGet] // mark as accepting only GET
        public ActionResult Create() // Index should probably provide some summary of people and hobbies
        {
            var model = new PersonHobbyViewModel();
            return View(model);
        }
    
        [HttpPost] // mark as accepting only POST
        public ActionResult Create(PersonHobbyViewModel model) {
    
            if (ModelState.IsValid) {
               var person = new Person { Name = model.Name };
               var hobby = new Hobby { Description = model.Description };
               person.Hobbies = new List<Hobby> { hobby };
    
               db.Persons.Add( person );
               db.SaveChanges();
            }
    
            return RedirectToAction( "details", new { id = person.Id } ); // view the newly created entity
        }
    }
    

    View

    @model MultipleFORMStest.PersonHobbyViewModel
    @{
        ViewBag.Title = "Create";
    }
    <h2>
        Create</h2>
    @using (Html.BeginForm("Create", "PersonHobby")) {
        <div>
            @Html.LabelFor(model => model.Person.Name)
            @Html.TextBoxFor(model => model.Person.Name)
            @Html.ValidationMessageFor(model => model.Person.Name)
            <input type="submit" value="Submit person" />
        </div>
    
        <div>
            @Html.LabelFor(model => model.Hobby.Description)
            @Html.TextBoxFor(model => model.Hobby.Description)
            @Html.ValidationMessageFor(model => model.Hobby.Description)
            <input type="submit" value="Submit hobby" />
        </div>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to better understand when I should and should not use Iterators.
I am trying to understand when we should use one over the other. From
I have been trying to understand what should be the right way in using
I'm trying to understand when I should use asynchronous web calls back to the
I'm trying to understand java APIs for digital signatures. I should use custom cryptoprovider
I am trying to understand how Lucene should be used. From what I have
I've been trying to understand for hours when I should use the viewDidload: and
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Just trying to understand that - I have never used it before. How is
I'm currently struggling to understand how I should organize/structure a class which I have

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.