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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:15:10+00:00 2026-05-11T15:15:10+00:00

I’m using an AJAX form to update an item to the database. When it

  • 0

I’m using an AJAX form to update an item to the database. When it gets done, it returns a partial view that re-lists all the items and displays them all in a table. The problem occurs when I have to add a modelstate error in my controller action. I don’t want to return the list of items when there is a modelstate error because I want to show the user the error using the ValidationMessage. My thinking is that I could do something like this in my controller:

       [AcceptVerbs(HttpVerbs.Post)]     public ActionResult UpdateNewsItem(int newsID, string newsTitle, string newsDescription, string newsBeginningDate, string newsEndingDate)     {         List<Models.News> lstNewsItem = new List<News>();          //we need to grab the member so we can capture the user id          //for the corresponding news property         MembershipUser member = Membership.GetUser(User.Identity.Name);          //the news instance to use in case the viewdata is invalid         Models.News newsError = new Models.News();          //create the datetime objects         DateTime dtBeginningDate = DateTime.MinValue;         DateTime dtEndingDate = DateTime.MaxValue;          //the message we want to send whenever the user enters an invalid date         string strInvalidDateError = 'Invalid date.  Please use a format like '12/25/2008'';          //clean user input         newsTitle = Models.clsGlobals.CleanString(newsTitle);         newsDescription = Models.clsGlobals.CleanParagraph(newsDescription);          //newsTitle          if (string.IsNullOrEmpty(newsTitle))         {             newsError.Title = string.Empty;             ModelState.AddModelError('newsTitle', 'You must enter a news title.');         }          //description         if (string.IsNullOrEmpty(newsDescription))         {             newsError.Description = string.Empty;             ModelState.AddModelError('newsDescription', 'You must enter a news description.');         }          //beginningDate         if (string.IsNullOrEmpty(newsBeginningDate))         {             ModelState.AddModelError('newsBeginningDate', 'You must enter a beginning date.');         }          //endingDate         if (string.IsNullOrEmpty(newsEndingDate))         {             ModelState.AddModelError('newsEndingDate', 'You must enter an ending date.');         }          //set the beginning date          try         {             dtBeginningDate = DateTime.Parse(newsBeginningDate);             newsError.BeginningDate = dtBeginningDate;         }          catch (FormatException)         {             ModelState.AddModelError('newsBeginningDate', strInvalidDateError);         }          //set the ending date         try         {             dtEndingDate = DateTime.Parse(newsEndingDate);             newsError.EndingDate = dtEndingDate;         }          catch (FormatException)         {             ModelState.AddModelError('newsEndingDate', strInvalidDateError);         }          //data is validated, so we can begin the update         if (ModelState.IsValid == true)         {              try             {                 //use to perform actions on db                 Models.NewsDataContext dcNews = new Models.NewsDataContext();                  //fetch the items that match what the user requested to edit                 lstNewsItem = this.GetNewsItem(newsID);                  //set news properties                 foreach (Models.News news in lstNewsItem)                 {                      news.UserId = (Guid)member.ProviderUserKey;                     news.Title = newsTitle;                     news.Description = newsDescription;                     news.EntryDate = DateTime.Now;                     news.BeginningDate = dtBeginningDate;                     news.EndingDate = dtEndingDate;                  }//next                  //update the transaction                 dcNews.SubmitChanges();                  //update the news list                 return PartialView('NewsList', this.GetNewsItems());              }              //just to make sure everything goes as planned,             // catch any unhandled exceptions             catch (Exception ex)             {                 ModelState.AddModelError('_FORM', ex);             }//end catch          }//end if valid modelstate           //invalid modelstate, so repopulate the viewdata and         //send it back          //the list to hold the entries         List<Models.News> lstErrorNewsItems = new List<Models.News>();          //set the remaining error properties         newsError.UserId = (Guid)member.ProviderUserKey;         newsError.NewsID = newsID;         newsError.EntryDate = DateTime.Now;          //add the item--there will only be one         //but the view is expecting a list so we will         //treat it like one         lstErrorNewsItems.Add(newsError);          return PartialView('EditNews', lstErrorNewsItems);      }//end actionresult 

The problem is that when a modelstate error occurs, the modelstate viewdata isn’t returned. I suspect it’s possibly because I’m not specifying an update target id. But I can’t set another updatetargetid because I already have one. Any ideas?

  • 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. 2026-05-11T15:15:11+00:00Added an answer on May 11, 2026 at 3:15 pm

    Alright, I’ve figured it out. Thanks for the response. For future reference, what I had to do was set up one master divider that all of my content goes into. Then, I always set the updateTargetID to that divider so that it doesn’t matter what content it displays, just that it displays it. This actually turns out to be easier because you don’t have to waste Javascript functions setting other div tags on and off because you’re only using one that is continuously updated.

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

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Update : nope, there is no redirect. screenshot Read RFC2388… May 12, 2026 at 12:13 am
  • Editorial Team
    Editorial Team added an answer The thing about XML Serialization is that it's not just… May 12, 2026 at 12:13 am
  • Editorial Team
    Editorial Team added an answer I did test your code and worked perfectly. I did… May 12, 2026 at 12:13 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.