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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:28:04+00:00 2026-05-14T03:28:04+00:00

I am lost on this MVC project I am working on. I also read

  • 0

I am lost on this MVC project I am working on. I also read Brad Wilsons article.
http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html

I have this:

public class Employee
{
    [Required]
    public int ID { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
}

and these in a controller:

public ActionResult Edit(int id)
{
    var emp = GetEmployee();
    return View(emp);
}

[HttpPost]
public ActionResult Edit(int id, Employee empBack)
{
    var emp = GetEmployee();
    if (TryUpdateModel(emp,new string[] { "LastName"})) {
        Response.Write("success");
    }
    return View(emp);
}

public Employee GetEmployee()
{
    return new Employee {
       FirstName = "Tom",
       LastName = "Jim",
       ID = 3
    };
}

and my view has the following:

<% using (Html.BeginForm()) {%>
    <%= Html.ValidationSummary() %>

    <fieldset>
        <legend>Fields</legend>    
        <div class="editor-label">
            <%= Html.LabelFor(model => model.FirstName) %>
        </div>
        <div class="editor-field">
            <%= Html.DisplayFor(model => model.FirstName) %>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.LastName) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxOrLabelFor(model => model.LastName, true)%>
            <%= Html.ValidationMessageFor(model => model.LastName) %>
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

<% } %>

Note that the only field editable is the LastName. When I postback, I get back the original employee and try to update it with only the LastName property. But but I see on the page is the following error:

•The FirstName field is required.

This from what I understand, is because the TryUpdateModel failed. But why? I told it to update only the LastName property.

I am using MVC2 RTM

Thanks in advance.

  • 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-14T03:28:04+00:00Added an answer on May 14, 2026 at 3:28 am

    The problem is that when your form gets posted back the FirstName field is empty. The issue is that since your’re passing the Employee as a parameter to your action, the validation occurs before you get a chance to make your call to GetEmployee(). You can do one of three things:

    1) Remove the [Required] attribute from your FirstName field.

    or

    2) Add Html.HiddenFor() for this field so it will make the round-trip. Like this:

    <%= Html.HiddenFor(model => model.FirstName) %>
    

    or

    3) Change your action declaration to:

    public ActionResult Edit(int id, FormCollection form)
    

    (3) is probably what you are looking for.

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

Sidebar

Related Questions

I'm completely new to JQuery and MVC. I'm working on a pet project that
I'm really lost on this case. I am trying to apply a custom MVC
I've taken the MVC introduction ( http://dev.sencha.com/deploy/ext-4.0.7-gpl/docs/index.html#!/guide/application_architecture ) and tried to modify it to
I have an MVC 3 project that I am working on using Entity Framework
Hi this is pretty basic jQuery stuff but i am a little lost. So
This is driving me crazy and has resulted in lost work (not much, at
OKay I lost alsmost the whole day on this. I have a webapp where
I am a bit lost as to how to explain this, so I will
This is probably something silly I'm missing but I'm definitely lost. I'm using .NET
This is probably really easy, but I'm lost on how to make sure it

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.