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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:59:06+00:00 2026-06-10T09:59:06+00:00

I am using EF4 (Db First) and I have an entity with a number

  • 0

I am using EF4 (Db First) and I have an entity with a number of non-nullable properties.

In an Edit form (Razor/MVC3), I want to allow the editing of only one of the properties, but not the others.

To get this to work, I am having to put @Html.HiddenFor(...) statements for each of my other properties that can’t be nullable, otherwise I get an error on SaveChanges().

Is there a simple way to just have the ID hidden on the view, the property that can be edited, and then update ONLY that 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-10T09:59:07+00:00Added an answer on June 10, 2026 at 9:59 am

    All you need to do in this case is to include the ID of the entity you are editing as a hidden field as well as a text field for the property that you actually wanna edit:

    @using (Html.BeginForm())
    {
        @Html.HiddenFor(x => x.ID)
        @Html.EditorFor(x => x.PropertyYouWannaEdit)
        <button type="submit">Update</button>
    }
    

    and then in the corresponding controller action you could retrieve the entity that needs to be edited from your database, update the value of the property that needs editing and save back the changes.

    [HttpPost]
    public ActionResult Update(SomeEntity model)
    {
        SomeEntity entityToEdit = db.GetEntity(model.ID);
        entityToEdit.PropertyYouWannaEdit = model.PropertyYouWannaEdit;
        db.Update(entityToEdit);
        return RedirectToAction("Success");
    }
    

    But personally I would use a view model and AutoMapper to handle this situation. So I would start by designing a view model representing the requirements of my view and including the properties that needs to be edited only:

    public class MyEntityViewModel
    {
        public int ID { get; set; }
        public string Property1ToBeEdited { get; set; }
        public string Property2ToBeEdited { get; set; }
        ...
    }
    

    and then have the corresponding view:

    @model MyEntityViewModel
    @using (Html.BeginForm())
    {
        @Html.HiddenFor(x => x.ID)
    
        @Html.EditorFor(x => x.Property1ToBeEdited)
        @Html.EditorFor(x => x.Property2ToBeEdited)
        ...
    
        <button type="submit">Update</button>
    }
    

    and finally the 2 controller actions (GET and POST):

    public ActionResult Update(int id)
    {
        // Fetch the domain model that we want to be edited from db
        SomeEntity domainModel = db.GetEntity(id);
    
        // map the domain model to a view model
        MyEntityViewModel viewModel = Mapper.Map<SomeEntity, MyEntityViewModel>(domainModel);
    
        // pass the view model to the view
        return View(viewModel);
    }
    
    [HttpPost]
    public ActionResult Update(MyEntityViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // validation failed => redisplay view so that the user can fix the errors
            return View(model);
        }
    
        // fetch the domain entity that needs to be edited:
        SomeEntity entityToEdit = db.GetEntity(model.ID);
    
        // update only the properties that were part of the view model,
        // leaving the others intact
        Mapper.Map<MyEntityViewModel, SomeEntity>(model, entityToEdit);
    
        // persist the domain model
        db.Update(entityToEdit);
    
        // we are done   
        return RedirectToAction("Success");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entity model build using EF4.1 code first, which uses a WCF
I am trying to teach my self MVC3 and EF4 using code first and
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
I started using EF 4.1 code first. I have a entity table like this:
Using EF 4.1 Code First, I have a Member entity and it in turn
I'm using EF4.3.1 code first, I have the code as below, namespace ConsoleApplication5 {
We are using Entity Framework 4.3 Code First. We have three databases. For each
New to Entity framework. I am using EF4 and I have implemented for now
I'm using the model-first approach offered by the EF4. I have two entities: User
I'm using EF4.1 with Code first and TPT (Table per Type) inheritance. I have

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.