For learning DisplayTemplates, I created a “String” DisplayTemplate as listed below. It is expected to append the word “Hello” after the model’s string value. But it is showing only the word “Hello”. How do we correct it?
Note: String.cshtml is added under 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)
<%= Html.Encode(Model) %> Hello!
@Darin. For the editor template, I used @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
@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
