Displayfor vs Editorfor [英] Displayfor vs Editorfor

查看:48
本文介绍了Displayfor vs Editorfor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC中EdiotFor和Displayfor(模板化HTML助手)有什么区别?

解决方案





EditorFor - 如果属性类型为基本类型(整数,字符串等),则该方法呈现< b>文本框的HTML输入元素



DisplayFor - 如果属性类型为基本类型(整数,字符串等),则该方法呈现字符串,表示属性值



例如如果我们有如下的EmployeeModel -

  public   class  EmployeeModel 
{
[显示(名称= 员工ID:)]
public int Id { get ; set ; }
[显示(名称= 员工姓名:)]
< span class =code-keyword> public
string 名称{ get ; set ; }
[显示(名称= DOJ:)]
[DisplayFormat (DataFormatString = {0:dd-mm-yyyy})]
public DateTime DateOfJoining { get ; set ; }
}





然后查看我们有,



 @ using(Html.BeginForm())
{
< h2 > EditorFor Implementation < / h2 >
@ Html.LabelFor(m => m.Id)
@ Html.EditorFor( m => m.Id)

@ Html.LabelFor(m => m.Name)
@ Html.EditorFor(m => m.Name)
//这将渲染输入框,其值为属性
@ Html.LabelFor(m => m.DateOfJoining)
@ Html.EditorFor(m => m.DateOfJoining)

< hr / >

< h2 > DisplayFor Implementation < / h2 >
@ Html.LabelFor(m => m.Id)
@Html。 DisplayFor(m => m.Id)

@ Html.LabelFor(m => m.Name)
@ Html.DisplayFor(m => m.Name)

//如果指定
@Html.LabelFor(m => m.DateOfJoining)
@ Html.DisplayFor(m =>),这将呈现带有格式化为DisplayFormat的值的​​字符串对象; m.DateOfJoining)
}





我不确定我是否可以在此附上截图供参考,但这就是我的意思从上面的代码查看来源。



对于EditorFor将呈现输入控件如下 -

 <   label       =  DateOfJoining >  DOJ:< span class =code-keyword><   / label  >  <  输入    class   =  text-box single-line    data-val   =  true    data-val-date   = 字段DOJ:必须是日期。    data-val-required   =  D OJ:字段是必需的。    id   =  DateOfJoining    name   =  DateOfJoining   类型  =  datetime    value   =  7 / 8/2015 12:00:00 AM    /  >  





对于DisplauFor,将呈现如下字符串 -

 <   label    =  DateOfJoining致敬>    >  DOJ:<   / label  >  08-00-2015 





希望这个帮助。


我认为你的问题与这篇文章有关。请看看这可能有助于您更好地了解您的问题。





http://stackoverflow.com/questions/6365633 / what-is-the-html-displayfor-syntax-for




What is the difference between EdiotFor and Displayfor(Templated HTML Helpers) in MVC?

解决方案

Hi,

EditorFor -If the property is typed as a primitive type (integer, string, and so on), the method renders an HTML input element for a text box

DisplayFor - If the property is typed as a primitive type (integer, string, and so on), the method renders a string that represents the property value

e.g. If we have EmployeeModel as below -

public class EmployeeModel
{
    [Display(Name = "Employee ID :")]
    public int Id { get; set; }
    [Display(Name = "Employee Name :")]
    public string Name { get; set; }
    [Display(Name = "DOJ :")]
    [DisplayFormat(DataFormatString = "{0:dd-mm-yyyy}")]
    public DateTime DateOfJoining { get; set; }
}



Then on view we have,

@using (Html.BeginForm())
{
    <h2>EditorFor Implementation</h2>
    @Html.LabelFor(m=>m.Id)
    @Html.EditorFor(m=>m.Id)
    
    @Html.LabelFor(m=>m.Name)
    @Html.EditorFor(m => m.Name)
    // This will render Input box with value of property
    @Html.LabelFor(m=>m.DateOfJoining)
    @Html.EditorFor(m => m.DateOfJoining) 

    <hr />

    <h2>DisplayFor  Implementation</h2>
    @Html.LabelFor(m=>m.Id)
    @Html.DisplayFor(m=>m.Id)
    
    @Html.LabelFor(m=>m.Name)
    @Html.DisplayFor(m=>m.Name)
    
     // This will render string object with value formatted as in DisplayFormat if specified
    @Html.LabelFor(m=>m.DateOfJoining)
    @Html.DisplayFor(m=>m.DateOfJoining)
}



I am not sure if I can attach screenshot here for reference, but this what i get from view source for above code.

For EditorFor will render input control as below -

<label for="DateOfJoining">DOJ :</label><input class="text-box single-line" data-val="true" data-val-date="The field DOJ : must be a date." data-val-required="The DOJ : field is required." id="DateOfJoining" name="DateOfJoining" type="datetime" value="7/8/2015 12:00:00 AM" />



For DisplauFor will render string as below -

<label for="DateOfJoining">DOJ :</label>08-00-2015



Hope this helps.


I think your question is related to this post. Please have a look this may help you to better understand about your question.


http://stackoverflow.com/questions/6365633/what-is-the-html-displayfor-syntax-for


这篇关于Displayfor vs Editorfor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆