How do you exclude properties from binding when calling UpdateModel()?

后端 未结 4 827
伪装坚强ぢ
伪装坚强ぢ 2020-12-20 11:46

I have a view model sent to the edit action of my controller. The ViewModel contains references to EntityObjects. (yea i\'m fine with it and don\'t need to want to duplicate

4条回答
  •  醉酒成梦
    2020-12-20 11:51

    A very simple solution that I figured out.

     try
    {
       UpdateModel(model, String.Empty, null, excludeProperties);
       ModelState.Remove("Entity.RetainedEarningsAccount.AccountNo");
       ModelState.Remove("Property.DiscountEarnedAccount.ExpenseCodeValue");
       ModelState.Remove("Entity.EntityAlternate.EntityID");
       ModelState.Remove("Property.BankAccount.BankAccountID");
       ModelState.Remove("Entity.PLSummaryAccount.AccountNo");
       ModelState.Remove("Property.RefundBank.BankAccountID");
       ModelState.Remove("ompany.Transmitter.TCC");
    
        if (ModelState.IsValid)
        {
           //db.SaveChanges();
        }
           return RedirectToAction("Index");
    }
    catch
    {
        return View(model);
    }
    

提交回复
热议问题