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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:39:07+00:00 2026-06-11T19:39:07+00:00

Model class public class product : BaseModel { [key] public string id { get;

  • 0

Model class

public class product : BaseModel
    {
        [key] public string id { get; set; }
        [Required] public string title { get; set; }
        [Required] public bool valid { set; get; }
        public decimal discount { get; set; }
        public string summery { get; set; }
        public string icon { get; set; }
        public DateTime date { get; set; }
    }

in View I just need update title column ,
here is model code

@using (Html.BeginForm()){
 @Html.HiddenFor(o => o.id);
 @Html.ValidationSummary(true)
 Title: @Html.EditorFor(model => model.title)
 @Html.ValidationMessageFor(model => model.title)
}

in Controller , product lost other values except id and title

according to this post one of the best solution is creating helper class for this specific form and use AutoMapper to map the helper to original model . but in my case ( over 60 models ) takes extra resources to implement helper classes. is there any better solution ?

  • 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-11T19:39:08+00:00Added an answer on June 11, 2026 at 7:39 pm

    in Controller , product lost other values except id and title

    That’s normal. What did you expect? Those are the only fields you have included in your HTML <form> so those are the only things you could ever hope to get back in your controller action. That’s how HTML works. Don’t expect miracles, ASP.NET MVC won’t just automagically set some values to the other fields of your model even if you haven’t included them in the form POST.

    Of course you could use the id that you received from the request to query your datastore and retrieve the corresponding model and do the respective processing on it.

    So:

    [HttpPost]
    public ActionResult SomeAction(int id)
    {
        var product = db.Products.Where(x => x.Id == id);
        if (!TryUpdateModel(product))
        {
            // validation failed => redisplay the view
            return View(model);
        }
    
        // at this stage the product variable will be updated with the title
        // property coming from the view and all other properties staying untouched
        // => you could persist the changes back to the database
        // I don't remember the syntax of this EF stuff - go read the manual
        // of how to persist the changes of this "product" instance back to the context
        // As far as I remember it was something like "db.SaveChanges" or something.
    
        ...
    
        // after a successfull UPDATE redirect in order to follow good practices of the
        // Redirect-After-Post pattern: http://en.wikipedia.org/wiki/Post/Redirect/Get
        return RedirectToAction("Success");
    }
    

    Be careful though with this approach though. I do not recommend it. An attacker could potentially set any properties like this by sending a specially crafted request. You should never pass your domain models to your views. You should always use view models.

    In order to protect against those mass-injection attacks you should keep a list of properties that are actually allows to be updated from the view:

    // ... only update the title property from the view
    if (!TryUpdateModel(product, new[] { "title" }))
    

    Now only the title property will be updated by the default model binder.


    The actual solution I recommend is to use a view model of course (I can’t keep rep[eating and stressing on this fact in the gazillions of answers I provide here on StackOverflow and I still see people using those EF autogenerated domain models in their views):

    So, yeah, define a view model, what does it cost you? A Ctrl+A on your Models folder and then:

    public class ProductViewModel
    {
        public int Id { get; set; }
        [Required]
        public string Title { get; set; }
    }
    

    Was it so difficult?

    And then navigate to your NuGet console and type: Install-Package AutoMapper. Then headaway to your Application_Start in Global.asax and define a mapping between your view model and your domain model (Actually it’s better to later externalize your mapping in AutoMapper Profiles):

    // gosh this EF crap generates lowercase classes => it sucks so badly
    // and violates all C# conventions
    Mapper.Map<ProductViewModel, product>();
    

    and finally your controller action becomes:

    [HttpPost]
    public ActionResult SomeAction(ProductViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }
    
        var product = db.Products.Where(x => x.Id == id);
        Mapper.Map<ProductViewModel, product>(model, product);
    
        // at this stage the product variable will be updated with the title
        // property coming from the view and all other properties staying untouched
        // => you could persist the changes back to the database
        // I don't remember the syntax of this EF stuff - go read the manual
        // of how to persist the changes of this "product" instance back to the context
        // As far as I remember it was something like "db.SaveChanges" or something.
    
        ...
    
        // after a successfull UPDATE redirect in order to follow good practices of the
        // Redirect-After-Post pattern: http://en.wikipedia.org/wiki/Post/Redirect/Get
        return RedirectToAction("Success");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following model class: public class Product { public int ProductID {get;set;}
I have this model: public class Package { public string CustomerName { get; set;
My model class is like this: Public class abc { public string p1 get;
My model: public class Father { Set<Son> sons = new HashSet<Son>(); String name =
Here's my model: public class StockRequestModel { public int StockID { get; set; }
I have the following Model: public class DeliveryTracking { public string TrackingRef { get;
I have the following model: public class Product { public int Id { get;
this is my object public class ProductContent { public Product Product { get; set;
My Model Class as follows: public class Employee { public Guid ID { get;
I have the following model: public class Tag { public int Id { get;

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.