This question already has answers here:
Successful Model Editing without a bunch of hidden fields (5 answers)
Closed 2 years ago.
I am writting an application with asp.net core 2.0 mvc.
Here is one of my controller's action:
[HttpPost]
public async Task MyAction(long id, [Bind("id_person,name")] Person p)
{
ViewBag.message = "";
if (p.name == "")
{
ViewBag.message = "You need to set name.";
}
else if (ModelState.IsValid == false)
{
ViewBag.message = string.Join("; ", ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));
}
else
{
mydb_context.Update(p);
await mydb_context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(p);
}
And here is the associated cshtml razor view:
@model myproject.Person
@ViewBag.message
Everything works fine: This action works great for insert and updating Persons.
Now, let's suppose i have a third field in my Person entity. For example "age".
I want this field to be secret. That's mean i do not want to user to see this information in his browser. I do not want too to put this information in an hidden field because the user may see or change the information.
My problem is if i keep MyAction and cshtml view as is, the age is set to null when a user updates a person. I want the age to keep its actual value in database.
Is there a basic way to do this ? I do not want to set by hand getters and setters in MyAction method, because i have a lot of fields. I want to work with Binding.
Thanks
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)