原文:
mvc中動態給乙個model類的屬性設定驗證
在mvc中有自帶的驗證機制,比如如果某個欄位的型別是數字或者日期,那麼使用者在輸入漢字或者英文本元時,那麼編譯器會自動驗證並提示使用者格式不正確,不過這樣的驗證畢竟功能有限,那麼就需要我們自己進行定製驗證。
假設有model類:class dinners
}public ienumerablegetruleviolations()
if (string.isnullorempty(description))
if (string.isnullorempty(hostedby))
if (string.isnullorempty(address))
if (string.isnullorempty(country))
if (string.isnullorempty(contactphone))
//這裡引用了類phonevalidator(自定義驗證類,不再詳述)
if (!phonevalidator.isvalidnumber(contactphone, country))
yield break;
}public class ruleviolation
public string propertyname
public ruleviolation(string errormessage, string propertyname)
}前台驗證**:
<%=html.validationsummary("please crrect the errors and try again.") %>
<% using (html.beginform()) ",model.eventdate)) %>
<%=html.validationmessage("eventdate","*") %>
description:
<%=html.textarea("description") %>
<%=html.validationmessage("description","*") %>
address:
<%=html.textarea("address") %>
<%=html.validationmessage("address","*") %>
country:
<%=html.textbox("country") %>
<%=html.validationmessage("country","*") %>
contactphone #:
<%=html.textbox("contactphone") %>
<%=html.validationmessage("contactphone","*") %>
latitude:
<%=html.textbox("latitude") %>
<%=html.validationmessage("latitude","*") %>
longitude:
<%=html.textbox("longitude") %>
<%=html.validationmessage("longitude","*") %>
<%= html.actionlink("back to list", "index") %>
注:**來自教程《一步一步學習asp.net mvc》
OC 動態方法決議給乙個類新增類方法
bool resolveclassmethod sel sel bool resolveinstancemethod sel sel void cx clsmethod void cx instancemethod id obj,sel sel,nsstring objc 再給乙個類或者物件傳送訊息...
Mongo如何動態更新乙個model的部分屬性
mongo裡面儲存乙個model,更新時想傳入乙個model去更新它,但是用mongo的update方法更新model時會更新其所有屬性。可是我只想更新不為null的屬性怎麼辦呢?而且屬性哪個為不為null還不確定,這就得需要構造動態更新了。上 private static string realt...
python 動態建立乙個類
class animal def eat self print eat class dog animal pass wangcai dog wangcai.eat 輸出 eat cat type cat animal,xiaohuamao cat xiaohuamao.eat 輸出 eat 元類就是...