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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:28:10+00:00 2026-05-16T23:28:10+00:00

I’m using MVC 2 and EF4. I have a view that displays my Application

  • 0

I’m using MVC 2 and EF4. I have a view that displays my Application (class) properties. Not all the properties are displayed in the view. There are a couple of the properties that need to be set once the submit button is clicked.

I’m getting client validation to pass, but my server validation is still failing. I receive an Application object in my CreateApplication action, I update a property, and do a ModelState.IsValid check. It is still false. I did a loop through my errors list and it displays the error text that I set on my SubmitterEmployeeNumber property using a Required data annotation. I did set it and I did update my model, but validation is still failing. Here is my code:

[HttpPost]
public ActionResult CreateApplication(Application application)
{
   application.SubmitterEmployeeNumber = "123456";

   TryUpdateModel(application);

   if (ModelState.IsValid)
   {
   }
}

Here is how I display the view:

public ActionResult CreateApplication()
{
   var viewModel = new ApplicationViewModel(new Application(), db.AccountTypes);

   return View(viewModel);
}

How do I get the validation to pass after I set the property after binding?

What is the difference between UpdateModel and TryUpdateModel and when do I need to use each?

EDIT:

I changed the name of the action to:

[HttpPost]
public ActionResult CreateApp()
{
   var application = new Application
   {
      ApplicationStateID = 1,
      SubmitterEmployeeNumber = "123456"
   };

   if (TryUpdateModel(application))
   {
      int success = 0;
   }
}

Here is my view:

<% using (Html.BeginForm("CreateApp", "Application")) {%>

TryUpdateModel still validates as false. I put in int success = 0; just to see if it will go into it but it doesn’t.

  • 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-16T23:28:11+00:00Added an answer on May 16, 2026 at 11:28 pm
    [HttpPost]
    public ActionResult CreateApplication()
    {
        var application = new Application 
        {
            SubmitterEmployeeNumber = "123456"
        };
        if (TryUpdateModel(application)) 
        {
            // The model is valid => submit values to the database
            return RedirectToAction("Success");
        }
        return View(application);
    }
    

    UPDATE: Due to many confusions in the comments section here’s a full working example.

    Model:

    public class Application
    {
        [Required]
        public int? ApplicationStateID { get; set; }
    
        [Required]
        public string SubmitterEmployeeNumber { get; set; }
    
        [Required]
        public string Foo { get; set; }
    
        [Required]
        public string Bar { get; set; }
    }
    

    Controller:

    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var application = new Application();
            return View(application);
        }
    
        [HttpPost]
        [ActionName("Index")]
        public ActionResult Create()
        {
            var application = new Application
            {
                ApplicationStateID = 1,
                SubmitterEmployeeNumber = "123456"
            };
            if (TryUpdateModel(application))
            {
                // success => update database, etc...
                return Content("yupee");
            }
    
            // failure => redisplay view to fix errors
            return View(application);
        }
    }
    

    View:

    <% using (Html.BeginForm()) { %>
        <div>
            <%: Html.LabelFor(x => x.Foo) %>
            <%: Html.TextBoxFor(x => x.Foo) %>
            <%: Html.ValidationMessageFor(x => x.Foo) %>
        </div>
    
        <div>
            <%: Html.LabelFor(x => x.Bar) %>
            <%: Html.TextBoxFor(x => x.Bar) %>
            <%: Html.ValidationMessageFor(x => x.Bar) %>
        </div>
    
        <input type="submit" value="GO GO" />
    <% } %>
    

    Hope this clears things up.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to

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.