温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET MVC中MvcContrib.FluentHtml如何使用

发布时间:2021-07-15 14:53:48 阅读:185 作者:Leah 栏目:编程语言

ASP.NET MVC中MvcContrib.FluentHtml如何使用,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

ASP.NET MVC

在MvcContrib.FluentHtml的应用中,我们随处可以见到下面的代码:

<%=this.TextBox(x=>x.Person.Name).Title("Entertheperson'sname").Label("Name:")%><br/>  ……  <%=this.Select(x=>x.Person.Gender).Options(Model.Genders).Size(5).Label("Gender:")  .Title("Selecttheperson'sgender")%><br/>  <labelidlabelid="Person_Name_Label"for="Person_Name">Name:</label> <inputidinputid="Person_Name"type="text"value="Jeremy"title="Entertheperson'sname"name="Person.Name"maxlength="50"/> .  <selectidselectid="Person_Gender"title="Selecttheperson'sgender"size="5"name="Person.Gender"> <optionvalueoptionvalue="M"selected="selected">Male</option> <optionvalueoptionvalue="F">Female</option> </select>


这种实现编程方式就是"Fluent Interface",这并不是什么新概念,2005年Eric Evans 和Martin Fowler就为这种实现方式命名.源文档 <http://www.martinfowler.com/bliki/FluentInterface.html> 可以通过维基百科中对Fluent Interface的描述获得一个基本的了解:In software engineering, a fluent interface (as first coined by Eric Evans and Martin Fowler) is a way of implementing an object oriented API in a way that aims to provide for more readable code.

我们分解上面的话:
◆它是面向对象API的一种实现方式
◆目的是增加代码的可读性

既然我们最熟悉的是StringBuilder,我们就从这个线索追下去:打开Reflector,很容易找到StringBuilder的Append方法:

publicStringBuilderAppend(stringvalue)  {  if(value!=null)  {  stringstringValue=this.m_StringValue;  IntPtrcurrentThread=Thread.InternalGetCurrentThread();  if(this.m_currentThread!=currentThread)  {  stringstringValue=string.GetStringForStringBuilder(stringValue,stringValue.Capacity);  }  intlength=stringValue.Length;  intrequiredLength=length+value.Length;  if(this.NeedsAllocation(stringValue,requiredLength))  {  stringnewString=this.GetNewString(stringValue,requiredLength);  newString.AppendInPlace(value,length);  this.ReplaceString(currentThread,newString);  }  else  {  stringValue.AppendInPlace(value,length);  this.ReplaceString(currentThread,stringValue);  }  }  returnthis;  }


了解了Fluent Interface,我们来看一下MVCContrib.FluentHTML的实现,这里以TextBox为例进行考察,首先看一下它的继承关系:

public class TextBox : TextInput< TextBox>

public abstract class TextInput< T> : Input< T>, ISupportsMaxLength where T : TextInput< T>

public abstract class Input< T> : FormElement< T> where T : Input< T>, Ielement

泛型是一种高层次的算法抽象,我们就通过Input< T>一窥端倪:

publicabstractclassInput<T>:FormElement<T>whereT:Input<T>,IElement  {  protectedobjectelementValue;  protectedInput(stringtype,stringname):base(HtmlTag.Input,name)  {  builder.MergeAttribute(HtmlAttribute.Type,type,true);  }  protectedInput(stringtype,stringname,MemberExpressionforMember,IEnumerable<IBehaviorMarker>behaviors)  :base(HtmlTag.Input,name,forMember,behaviors)  {  builder.MergeAttribute(HtmlAttribute.Type,type,true);  }  ///<summary> ///Setthe'value'attribute.  ///</summary> ///<paramnameparamname="value">Thevaluefortheattribute.</param> publicvirtualTValue(objectvalue)  {  elementValue=value;  return(T)this;  }  ///<summary> ///Setthe'size'attribute.  ///</summary> ///<paramnameparamname="value">Thevaluefortheattribute.</param> publicvirtualTSize(intvalue)  {  Attr(HtmlAttribute.Size,value);  return(T)this;  }  protectedoverridevoidPreRender()  {  Attr(HtmlAttribute.Value,elementValue);  base.PreRender();  }  } 

关于ASP.NET MVC中MvcContrib.FluentHtml如何使用问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI