简体   繁体   English

ASP.NET MVC3:DisplayTemplates不显示模型值(MVC3部分页)

[英]ASP.NET MVC3: DisplayTemplates does not show model value (MVC3 Partial Page)

For learning DisplayTemplates, I created a “String” DisplayTemplate as listed below. 为了学习DisplayTemplates,我创建了一个“ String” DisplayTemplate,如下所示。 It is expected to append the word “Hello” after the model's string value. 预计在模型的字符串值后添加单词“ Hello”。 But it is showing only the word “Hello”. 但是它仅显示“ Hello”一词。 How do we correct it? 我们如何纠正它?

Note: String.cshtml is added under Views\\Contact\\DisplayTemplates 注意:String.cshtml添加在Views \\ Contact \\ DisplayTemplates下

public class Contact
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

CONTROLLER 控制器

public class ContactController : Controller
{

    // GET: /Contact/Details/5
    public ActionResult Details(int id)
    {
        Contact myContact = new Contact();
        myContact.FirstName = "Lijo";
        myContact.LastName = "Cheeran";
        myContact.Age = 26;

        return View(myContact);

    }

}

Detail View 详细视图

@model MyDisplayAndEditorTemplateTEST.Contact

<fieldset>
<legend>Contact</legend>

<div class="display-label" style="font-weight:bold" >FirstName</div>
<div class="display-field">
    @Html.DisplayFor(model => model.FirstName)
</div>

<div class="display-label" style="font-weight:bold">LastName</div>
<div class="display-field">
    @Html.DisplayFor(model => model.LastName)
</div>

<div class="display-label" style="font-weight:bold">Age</div>
<div class="display-field">
    @Html.DisplayFor(model => model.Age)
</div>

Partail Page for String DisplayTemplate (String.cshtml) 字符串DisplayTemplate的Partail页面(String.cshtml)

<%= Html.Encode(Model) %> Hello!

@Darin. @Darin。 For the editor template, I used @Html.EditorFor(model => model.FirstName). 对于编辑器模板,我使用了@ Html.EditorFor(model => model.FirstName)。 Still it is coming as a label as shown below. 仍然作为标签显示,如下所示。 How do we change it to get it as a textbox? 我们如何更改它以使其成为文本框?

Details.cshtml Details.cshtml

@model MyDisplayAndEditorTemplateTEST.Contact

<fieldset>
<legend>Contact</legend>

<div>FirstName</div>
<div>
     @Html.EditorFor(model => model.FirstName)
</div>

<div class="display-label" style="font-weight:bold">LastName</div>
<div class="display-field">
    @Html.DisplayFor(model => model.LastName)
</div>

EditorTemplate 编辑器模板

@Model TEST

DisplayTemplate 显示模板

Hello! @Model

在此处输入图片说明

<%= Html.Encode(Model) %> Hello! is WebForms syntax. 是WebForms语法。 Make sure you are not confusing the 2 view engines. 确保您不会混淆2个视图引擎。 So put the following in your string.cshtml Razor display template: 因此,将以下内容放入您的string.cshtml Razor显示模板中:

@Model Hello!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM