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

  • Home
  • SEARCH
  • 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 8108059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:58:33+00:00 2026-06-06T00:58:33+00:00

I hope this is an easy one. Using MVC4 with Entity Framework 4.1. I’am

  • 0

I hope this is an easy one.

Using MVC4 with Entity Framework 4.1.

I’am trying to update a model with some properties passed through from a View with a custom view model’s properties.

I have this class.

public class SitePage
{
    public System.Guid PageID { get; set; }
    public int PageTypeID { get; set; }
    public string PageTitle { get; set; }
    public Nullable<int> RegionId { get; set; }
    public Nullable<int> LockLevelId { get; set; }
    public System.DateTime UpdatedDate { get; set; }
    public System.Guid UpdatedById { get; set; }
    public System.Guid CreatedById { get; set; }
    public System.DateTime CreatedDate { get; set; }
    public string Url { get; set; }
    public int PageStateId { get; set; }
    public string SummaryImage { get; set; }

}

My controller

public ActionResult Update(AddPageViewModel model)
{
 if (ModelState.IsValid)
{
  var g = new Guid(model.PageId);
  SitePage UpdatePage = dbSite.SitePages.FirstOrDefault(m => m.PageID == g);

           //This is a page update. Not updating all properties
            UpdatePage.PageTitle = model.PageTitle;
            UpdatePage.GetFirstSection.PageSectionText.PageText = model.PageText;
            UpdatePage.PageTypeID = (int)SitePageType.PageTypes.CommunityPage;
            UpdatePage.RegionId = model.Region
            UpdatePage.CreatedById = userId;
            UpdatePage.CreatedDate = DateTime.UtcNow;
            UpdatePage.UpdatedById = userId;
            UpdatePage.UpdatedDate = DateTime.UtcNow;

 TryUpdateModel(UpdatePage);
 dbSite.Save();

 return RedirectToAction("Community_test", "Community", new { id = UpdatePage.Url });

}
else
{
     return RedirectToAction("Community_test", "Community");
}
}

Passing this page view model to the controller

public class AddPageViewModel
{
   public string PageTitle { get; set; }
   public string PageText { get; set; }
   public int Region { get; set; }
   public string TagString { get; set; }
   public string ParentPageId { get; set; }
   public string PageId { get; set; }
}

If i debug I can see that the SitePage instance UpdatePage has all the correct values when TryUpdateModel is called. However all values except RegionId are updated correctly in the db. RegionId gets set as null. If i run a trace on the server the sql entity framework generates is indeed setting RegionId to null even though my model has a value. This problem is intermittent! Some times it works, other it doesnt. 1 in 4 will update correctly and i cant quite see where im going wrong. Im newish to this stuff really.

Our classes are generated from the database and we are using an edmx file. Im not sure if i can make any changes to these classes without them just being overridden the next time we update model from database.

Any ideas or more info needed, give me a shout. Cheers

  • 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-06T00:58:35+00:00Added an answer on June 6, 2026 at 12:58 am

    TryUpdateModel takes the values from the form and populate the given model. This means that setting any values on UpdatePage isn’t necessary.

    if (ModelState.IsValid)
    {
        var updatePage = dbSite.SitePages.FirstOrDefault(m => m.PageID == model.PageId);
    
        TryUpdateModel(updatePage);
        dbSite.Save();
    
        //...
    }
    

    It is good practice to also specify the properties that should be updated.

    TryUpdateModel(updatePage, new string[] { "PageTitle", "RegionId", etc.. });
    

    Regarding RegionId getting set to Null.

    If i debug I can see that the SitePage instance UpdatePage has all the correct values when TryUpdateModel is called.

    Before TryUpdateModel is called or right after?

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

Sidebar

Related Questions

I hope this is an easy one, but I've got a page where there's
I hope this is any easy one. I have a view that I want
I hope this is easy to spot. This query has a synatx error; public
hope this will be a quick easy question, I've just tried my app on
I hope this is a relatively easy problem although I have spent hours websearching
I'm stuck in this very easy problem (I hope it is for you). I
Hope someone has an easy answer on this. I have a header image which
I'm new to Quartz2d so I hope this is an easy question to answer.
Hope some of you might have faced this requirement:- I have desktop based product,
Hope this is the right place to be asking this, so my apologies if

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.