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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:43:40+00:00 2026-06-04T14:43:40+00:00

public class SearchForm { //Note: Property is nullable public DateTime? CurrentViewDate {get;set;} public DateTime

  • 0
public class SearchForm {

     //Note: Property is nullable 
     public DateTime? CurrentViewDate {get;set;}       
     public DateTime  StartDate {get;set;}
}

//In the controller     
//[GET] 
public ActionResult Index()
{
}

//[POST]    
public ActionResult Index(SearchForm formModel)
{
     if(formModel.CurrentViewDate == null)            
        formModel.CurrentViewDate = DateTime.Now.AddDays(1d);
     else
        formModel.CurrentViewDate = formModel.CurrentViewDate.AddDays(1d);

    formModel.StartDate = DateTime.Now;   

}


//In view
@Html.HiddenFor(c => c.CurrentViewDate).
<input id="SearchForm_CurrentViewDate" name="SearchForm.CurrentViewDate" 
       type="hidden" value="5/25/2012 11:59:59 PM" />
<input type="submit" name="btnNext" id="btnNext" value="Go Increment" />

But, I when click submit the value does gets incremented but the hidden for field holds the previous value and the only way it displays correct value is
If do this @Html.HiddenFor(c => c.CurrentViewDate.Value) , but then on the
re-post the formModel.CurrentViewDate has null value since the binding names are different.

Any ideas? It feels like it fetches the date time value from StartDate property instead of CurrentViewDate property

  • 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-04T14:43:41+00:00Added an answer on June 4, 2026 at 2:43 pm

    You need to remove the value from the ModelState if you intend to modify it:

    public ActionResult Index(SearchForm formModel)
    {
        ModelState.Remove("CurrentViewDate");
    
        if(formModel.CurrentViewDate == null)            
        {
            formModel.CurrentViewDate = DateTime.Now.AddDays(1d);
        }
        else
        {
            formModel.CurrentViewDate = formModel.CurrentViewDate.AddDays(1d);
        }
    
        return View(model);
    }
    

    The reason you need to do this is because all Html helpers such as TextBoxFor, HiddenFor, CheckBoxFor, … first look in the ModelState when binding their values and after that they look in the model. This behavior is by design. So since in your POST action there’s already a value in the ModelState (the one that was sent with the request) it is this value that’s being used.

    Everytime you attempt to modify some property on your view model inside a POST controller action and expect this change to reflect in the value don’t forget about the ModelState.

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

Sidebar

Related Questions

public class Options { public FolderOption FolderOption { set; get; } public Options() {
public class Emp { public int ID { get; set; } public string Name
I have the following code in controller: public ActionResult SearchPage() { return View(); }
I have the following property on a class persisted with NHibernate: public virtual string
Here is my search function : class Ads extends Controller { public function search($format='velos',
public class A { //some fields (may not only primitive types) and methods here}
public class TestBL { public static void AddFolder(string folderName) { using (var ts =
public class UploadHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { ... // Create
public class ToolPartGetLists : Microsoft.SharePoint.WebPartPages.WebPart, ICommunicationInterface { private bool _error = false; //......... protected
public class Order { static Customer cust = new Customer(); string sEmpty = ;

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.