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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:06:27+00:00 2026-05-18T09:06:27+00:00

I have a ASP.NET MVC 2.0 application using Entity Framework. All my views use

  • 0

I have a ASP.NET MVC 2.0 application using Entity Framework. All my views use view models, most of them complex. Meaning…the object to be edited is a property of the view model, and not the view model itself.

I am using partial classes with data annotations, and checking ModelState.IsValid inside the POST actions in the controller.

I have a “NEW” form and an “EDIT” form for a simple object with 3 fields!

The ModelState.IsValid check works on the NEW form, and shows the correct “required field” errors, if I try to submit a blank form.

But if I load an EDIT form, and clear the values from some textboxes that are required, and submit the form, I do NOT get validation errors, I just get an exception:

Error executing child request for handler ‘System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerWrapper’.

So my question is, does ModelState.IsValid not work with an EDIT form, since perhaps it’s looking at the values from the view model object that were loaded, instead of the FormCollection?



    // this one does not validate

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int accountStatusKey, AccountStatusEditViewModel model, FormCollection values)
        {
            if (ModelState.IsValid)
            {
                db.UpdateAccountStatus(accountStatusKey, values);
                return RedirectToAction("States");
            }
            else
            {
                return View("Edit", model);
            }
        }


    // this one does validate

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult New(AccountStatusNewViewModel model, FormCollection values)
        {
            if (ModelState.IsValid)
            {
                db.AddAccountStatus(values);

                return View("States", new AccountStatusStatesViewModel());
            }
            else
            {
                return View("New", model);
            }
        }

    // how I arrive AT the edit form

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(int accountStatusKey)
        {
            return View("Edit", new AccountStatusEditViewModel(accountStatusKey));
        }

    // and finally, the view model code

    public class AccountStatusEditViewModel : ViewModelBase
    {

        public AccountStatus AccountStatus { get; private set; }

        public IEnumerable States { get; private set; }

        public List StatusTypes { get; private set; }

        public AccountStatusEditViewModel(int accountStatusKey)
        {
            AccountStatus = db.GetAccountStatusByKey(accountStatusKey);
            States = db.GetAllStates();

            StatusTypes = new List();
            StatusTypes.Add("Primary Status");
            StatusTypes.Add("Secondary Status");
            StatusTypes.Add("External Status");
        }

        public AccountStatusEditViewModel()
        {
        }

    }

    // this action method does not work at all either - no db updating, no validation
    // the page simply redirects to "States" view, which should only happen if the db
    // was being updated, right?  But nothing is changing in the DB, and the validation
    // never happens.

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(AccountStatusEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (TryUpdateModel(model, "AccountStatus"))
                {
                    return RedirectToAction("States");
                }
                else
                {
                    return View("Edit", model);
                }
            }
            else
            {
                return View("Edit", model);
            }

        }


  • 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-18T09:06:27+00:00Added an answer on May 18, 2026 at 9:06 am

    Since the 2.0 version of MVC i’m not using the formcollection anymore.

    I only use the viewmodel in the action’s parameter when i have a post, like this:

    [HttpPost]
    public ActionResult Activatecard(ActivateCardViewModel model)
    {
    

    When ‘it’ can’t create my model (entered blabla for a datetime field, or when the validations are not met (i use the validation attributes from the System.ComponentModel.DataAnnotations namespace) i get the ModelState.IsValid equals to false.

    I created a blank asp.net mvc2 app, and this was the model that the standard template used for the logon action.

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

Sidebar

Related Questions

No related questions found

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.