Conditional validation on model in MVC

后端 未结 4 427
半阙折子戏
半阙折子戏 2020-12-01 06:39

I have a view & model that I use for both the edit and the insert page for a record. One of the business requirements is that a certain field is required on edit but not

4条回答
  •  忘掉有多难
    2020-12-01 07:21

    You can try on validation with:

    ModelState.Remove("ProposedCost");
    

    Or extending your model like this:

    public class NewModeViewModel : EditModeViewModel
    {
       public new decimal ProposedCost { get; set; }
    }
    

    and passing to the edit view.

提交回复
热议问题