簡體   English   中英

在MVC中自定義驗證模型對象

[英]Custom validating a Model object in MVC

我需要在控制器中的mvc中驗證某些內容

@Html.ValidationMessageFor(model => model.somestring)// in the view

if (model.string = some condition)
ModelState.AddModelError("somestring", "String cannot be empty");// in the controller

但是如果我認為我有一個自定義對象,例如

@Html.ValidationMessageFor(model => model.someobject.somestring)// in the view

如何驗證? 以下語法正確嗎?

if (model.someobject.somestring = some condition)
ModelState.AddModelError("somestring", "String cannot be empty");// in the controller

指定密鑰時,您需要確保使用屬性的完整路徑:

ModelState.AddModelError("someobject.somestring", "String cannot be empty);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM