1

I am writting an application with asp.net core 2.0 mvc.

Here is one of my controller's action:

    [HttpPost]
    public async Task<IActionResult> 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

<form asp-action="MyAction">
    <div>@ViewBag.message</div>
    <input type="hidden" asp-for="id_person" />
    <input asp-for="name" />
    <input type="submit">
</form>

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

DavidG
  • 113,891
  • 12
  • 217
  • 223
Bob5421
  • 7,757
  • 14
  • 81
  • 175
  • If you don't want it to be visible or changed, why are you putting it on the page in the first place? – DavidG Dec 14 '17 at 15:20
  • The field i want to hide is age (not id_person) – Bob5421 Dec 14 '17 at 18:11
  • You still didn't answer the question.... – DavidG Dec 14 '17 at 18:20
  • The field age does not appear in the form – Bob5421 Dec 14 '17 at 20:25
  • You're really not making a lot of sense I'm afraid. I give up. – DavidG Dec 14 '17 at 20:58
  • Let imagine there is another field in my table which i do not want to see it in the form – Bob5421 Dec 15 '17 at 11:36
  • 2
    As long as the HttpPost Controller action doesn't explicitly update the hidden field to the db/etc., there shouldn't be a problem (whether it's changed by the user or not). Instead of your "mydb_context.Update(p), you should be only updating the fields you want to allow editing of, not the whole model at once. – David Dec 18 '17 at 19:19

1 Answers1

0

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://learn.microsoft.com/fr-fr/ef/ef6/saving/change-tracking/property-values

cnz018
  • 11
  • 3