来吧,自己努力一点,说说你遇到了什么困难!否则你怎么能学到东西?
Views/Home/Index.cshtml:
@model SampleModel
<h3>Details</h3>
<fieldset style="padding: 1em; margin: 0; border: solid 1px #999;">
@Html.DisplayForModel()
</fieldset>
<p>@Html.ActionLink("Edit", "Edit")</p>
Views/Home/Edit.cshtml:
@model SampleModel
<h3>Edit</h3>
@using (Html.BeginForm())
{
<fieldset style="padding: 1em; margin: 0; border: solid 1px #999;">
@Html.ValidationSummary("Broken stuff:")
@Html.EditorForModel()
<input type="submit" value=" Submit " />
</fieldset>
}
<p>@Html.ActionLink("Details", "Index")</p>
Views/Shared/DisplayTemplates/Object.cshtml:
@model object
@if (Model == null)
{
@ViewData.ModelMetadata.NullDisplayText
}
else if (ViewData.TemplateInfo.TemplateDepth > 1)
{
@ViewData.ModelMetadata.SimpleDisplayText
}
else
{
<table cellpadding="0" cellspacing="0" border="0">
@foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm)))
{
if (prop.HideSurroundingHtml)
{
@Html.Display(prop.PropertyName)
}
else
{
<tr>
<td>
<div class="display-label" style="text-align: right;">
@prop.GetDisplayName()
</div>
</td>
<td>
<div class="display-field">
@Html.Display(prop.PropertyName)
</div>
</td>
</tr>
}
}
</table>
}
Views/Shared/EditorTemplates/Object.cshtml:
@model object
@if (ViewData.TemplateInfo.TemplateDepth > 1)
{
@ViewData.ModelMetadata.SimpleDisplayText
}
else
{
<table cellpadding="0" cellspacing="0" border="0">
@foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm)))
{
if (prop.HideSurroundingHtml)
{
@Html.Editor(prop.PropertyName)
}
else
{
<tr>
<td>
<div class="editor-label" style="text-align: right;">
@(prop.IsRequired ? "*" : "")
@Html.Label(prop.PropertyName)
</div>
</td>
<td>
<div class="editor-field">
@Html.Editor(prop.PropertyName)
@Html.ValidationMessage(prop.PropertyName, "*")
</div>
</td>
</tr>
}
}
</table>
}