in Education by
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)

1 Answer

0 votes
by
I don't know if it's still available in EFCore but I used to do this in EF6. If you only want to update the Name propoerty, you can use mydb_context.Entry(). var person = mydb_context.Person.FirstOrDefault(p => p.id_person == id); if(person != null) { mydb_context.Entry(person).Property("name").CurrentValue = "Toto"; mydb_context.SaveChanges(); } Read this for more details https://docs.microsoft.com/fr-fr/ef/ef6/saving/change-tracking/property-values

Related questions

0 votes
    I'm new to C# and feel like I am losing my mind. I cannot get pass this compilation error. The ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    I have the following WEB API method, and have a SPA template with Angular: [HttpPost] public IActionResult ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I am developing a .NET core application with Identity login which provides a Bootstrap form In the /Identity ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I have a database (SQL Server) with clients and a MVC plataform, and i want a row to be "hidden ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have a database (SQL Server) with clients and a MVC plataform, and i want a row to be "hidden ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I am inserting JSON string into table, than on listing page in View inside foreach loop I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    In Azure, I have a small app deployed: https://jsnamespace.azurewebsites.net/. In localhost, it works ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    In Azure, I have a small app deployed: https://jsnamespace.azurewebsites.net/. In localhost, it works ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    i create this class and it have this constructor : public class ReturnResult : ImplicitReturnResult { public bool ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    So the problem is i don't get get a value from textbox field on GridView RowUpdating event. Here ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I have a master page with a form element and the defaultbutton attribute set to a server-side ImageButton. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    I have a Login control placed inside LoginView control contained within a PANEL. I want to set login button ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    I'm working on a DNN skin using Christoc's DNN templates and when I create a normal ASP.NET ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    Say I have an ASMX web service, MyService. The service has a method, MyMethod. I could execute MyMethod ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 31, 2022 in Education by JackTerrance
0 votes
    I've got a Sharepoint WebPart which loads a custom User Control. The user control contains a Repeater which ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 22, 2022 in Education by JackTerrance
...