Add sample for Display Html Helper.

Showcased a lot of the core pieces of Display in the sample.  Some pieces are still missing such as DataType handling.  The infrastructure is there to handle it in the TemplateRenderer but the model metadata providers do not construct the metadata with the data types.
This commit is contained in:
N. Taylor Mullen 2014-04-02 12:55:34 -07:00
parent 8477f47632
commit 5dbd006165
7 changed files with 100 additions and 2 deletions

View File

@ -75,7 +75,16 @@ namespace MvcSample.Web
User user = new User()
{
Name = "My name",
Address = "My address"
Address = "My address",
Alive = true,
Age = 13,
GPA = 13.37M,
Dependent = new User()
{
Name = "Dependents name",
Address = "Dependents address",
Alive = false,
},
};
return user;

View File

@ -9,5 +9,8 @@ namespace MvcSample.Web.Models
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
public decimal GPA { get; set; }
public User Dependent { get; set; }
public bool Alive { get; set; }
}
}

View File

@ -0,0 +1,20 @@
@functions {
private bool? Value {
get {
if (ViewData.Model == null) {
return null;
}
return Convert.ToBoolean(ViewData.Model, System.Globalization.CultureInfo.InvariantCulture);
}
}
}
@if (ViewData.ModelMetadata.IsNullableValueType) {
<select class="list-box tri-state" disabled="disabled">
<option value=""@(Value.HasValue ? "" : "selected='selected'")>Not Set</option>
<option value="true"@(Value.HasValue && Value.Value ? "selected='selected'" : "")>True</option>
<option value="false"@(Value.HasValue && !Value.Value ? "selected='selected'" : "")>False</option>
</select>
} else {
<input type="checkbox" class="check-box" disabled="disabled" @(Value.HasValue && Value.Value ? "checked='checked'" : "") />
}

View File

@ -0,0 +1,11 @@
@functions {
private object FormattedValue {
get {
if (ViewData.TemplateInfo.FormattedModelValue == ViewData.ModelMetadata.Model) {
return String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:0.00}", ViewData.ModelMetadata.Model);
}
return ViewData.TemplateInfo.FormattedModelValue;
}
}
}
@Html.Encode(FormattedValue)

View File

@ -0,0 +1 @@
@ViewData.TemplateInfo.FormattedModelValue

View File

@ -0,0 +1 @@
@Html.Encode(ViewData.TemplateInfo.FormattedModelValue)

View File

@ -204,7 +204,60 @@
</form>
</div>
<div style="float: left; border: 5px solid green;">
<table>
<tr>
<td>
<label class="control-label col-md-2">Display Name</label>
</td>
<td>
'@Html.Display("Name")'
</td>
</tr>
<tr>
<td>
<label class="control-label col-md-2">Display Dependent.Name</label>
</td>
<td>
'@Html.Display("Dependent.Name")'
</td>
</tr>
<tr>
<td>
<label class="control-label col-md-2">Display Alive</label>
</td>
<td>
'@Html.Display("Alive")'
</td>
</tr>
<tr>
<td>
<label class="control-label col-md-2">Display Dependent.Alive</label>
</td>
<td>
'@Html.Display("Dependent.Alive")'
</td>
</tr>
<tr>
<td>
<label class="control-label col-md-2">Display Age</label>
</td>
<td>
'@Html.Display("Age")'
</td>
</tr>
<tr>
<td>
<label class="control-label col-md-2">Display GPA</label>
</td>
<td>
'@Html.Display("GPA")'
</td>
</tr>
</table>
</div>
<div style="float: right; border: 5px solid red;">
@await Component.InvokeAsync("Tags", 15)
</div>
</div>
</div>