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 ?
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
idthat you received from the request to query your datastore and retrieve the corresponding model and do the respective processing on it.So:
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:
Now only the
titleproperty 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:
Was it so difficult?
And then navigate to your NuGet console and type:
Install-Package AutoMapper. Then headaway to yourApplication_StartinGlobal.asaxand define a mapping between your view model and your domain model (Actually it’s better to later externalize your mapping in AutoMapper Profiles):and finally your controller action becomes: