接上篇…首先讓我們來看如何創建一個我們先前討論的textbox
我們已經知道需要創建的textbox有一個文本值與之對應:
文本值在label標簽中
可選的值放在Textbox中
可選的驗證信息(validation message)
如果上面3個條件都能滿足,肯定也能滿足我們在part1裡的那5個條件.還有一些錦上添花的是可以通 過屬性來指定textbox是否包裹在li標簽內以及textbox是否是readonly模式.這樣我們便能更好的在view page中代碼復用。下面的代碼包含所有HtmlText(譯者按:繼承AbstractHtmlViewObject對象,在part1的 類圖中)對象所有的屬性:
private readonly string mLabelText;
private readonly bool mCreateLabel;
private readonly object mValue;
private readonly string mValidationMessage;
private readonly bool mCreateValidationMessage;
private readonly bool mCreateLi;
private readonly bool mReadonly;
public HtmlText(
ViewRequestContext requestContext, string name, string labelText, objec
string validationMessage, bool @readonly, bool createLi, object attribu
: base(requestContext, name)
{
mLabelText = labelText;
mCreateLabel = !string.IsNullOrEmpty(mLabelText);
mValidationMessage = validationMessage;
mCreateValidationMessage = !string.IsNullOrEmpty(validationMessage);
mCreateLi = createLi;
mReadonly = @readonly;
Attributes = attributes;
object valueToAssign = value;
if (valueToAssign == null)
{
// see if the ModelState has a value for this
valueToAssign = GetModelStateValue(name, typeof(string));
}
mValue = valueToAssign;
}