ASP.NET MVC - Alternative for [Bind(Exclude = “Id”)]

前端 未结 5 2178
执笔经年
执笔经年 2020-12-05 00:42

Is there an alternative for [Bind(Exclude = \"Id\")] (Related Question) ?

Could I write a model binder?

5条回答
  •  一个人的身影
    2020-12-05 01:24

    As an addition to the existing answers, C# 6 makes it possible to exclude the property in a safer way:

    public ActionResult Edit(Person person)
    {
        ModelState.Remove(nameof(Person.Id));
    
        if (ModelState.IsValid)
           {
               //Save Changes;
           }
        }
    }
    

    or

    public ActionResult Index([Bind(Exclude = nameof(SomeDomainModel.Id))] SomeDomainModel model)
    

提交回复
热议问题