Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
193 lines (161 sloc) 8.03 KB
Partials.cshtml
@model ViewModelExample
@{
ViewBag.Title = "Partials";
}
<h1>Partials</h1>
@using (var f = Html.BeginChameleonForm())
{
@f.Partial("_ParentPartial")
@f.Partial("_BaseParentPartial")
@f.PartialFor(m => m.Child, "_ChildPartial")
@f.PartialFor(m => m.BaseChild, "_BaseChildPartial")
using (var s = f.BeginSection("This is in the parent view"))
{
@s.FieldFor(m => m.Decimal).Append("in parent view")
@s.Partial("_ParentPartial")
@s.Partial("_BaseParentPartial")
@s.FieldFor(m => m.ListId).Append("in parent view")
@s.PartialFor(m => m.Child, "_ChildPartial")
@s.PartialFor(m => m.BaseChild, "_BaseChildPartial")
@s.FieldFor(m => m.SomeCheckbox).Append(" in parent view")
}
using (var n = f.BeginNavigation())
{
@n.Submit("Submit")
}
}
=====
_ParentPartial.cshtml
@model ViewModelExample
@if (this.IsInFormSection())
{
@:@this.FormSection().FieldFor(m => m.TextAreaField).Append("from partial against top-level model")
}
else
{
using (var s = this.Form().BeginSection("This is from a form-level partial against the top-level model")) {
@s.FieldFor(m => m.Int)
}
}
=====
_ChildPartial.cshtml
@model ChildViewModel
<p>This should show the ID of the child field including the parent path: @Html.IdFor(m => m.ChildField)</p>
@if (this.IsInFormSection())
{
@:@this.FormSection().FieldFor(m => m.ChildField).Append("From partial against child model")
}
else
{
using (var s = this.Form().BeginSection("This is from a form-level partial against a child model")) {
@s.FieldFor(m => m.SomeEnum)
}
}
=====
_BaseParentPartial.cshtml
@model ViewModelExampleBase
@if (this.IsInFormSection())
{
@:@this.FormSection().FieldFor(m => m.BaseField).Append("from partial against top-level model casted to base class")
}
else
{
using (var s = this.Form().BeginSection("This is from a form-level partial against the top-level model casted to base class")) {
@s.FieldFor(m => m.BaseField2)
}
}
=====
_BaseChildPartial.cshtml
@model ViewModelExampleBaseChild
@if (this.IsInFormSection())
{
@:@this.FormSection().FieldFor(m => m.BaseChildField).Append("From partial against child model in the base class")
}
else
{
using (var s = this.Form().BeginSection("This is from a form-level partial against a child model in the base class")) {
@s.FieldFor(m => m.BaseChildField2)
}
}
=====
Rendered Source
<form action="" method="post" novalidate="novalidate"> <fieldset>
<legend>This is from a form-level partial against the top-level model</legend>
<dl>
<dt><label for="Int">Int</label> <em class="required">*</em></dt>
<dd>
<input data-val="true" data-val-number="The field Int must be a number." data-val-required="The Int field is required." id="Int" name="Int" type="text" value="0"> <span class="field-validation-valid" data-valmsg-for="Int" data-valmsg-replace="true"></span>
</dd>
</dl>
</fieldset>
<fieldset>
<legend>This is from a form-level partial against the top-level model casted to base class</legend>
<dl>
<dt><label for="BaseField2">Base field 2</label></dt>
<dd>
<input id="BaseField2" name="BaseField2" type="text" value=""> <span class="field-validation-valid" data-valmsg-for="BaseField2" data-valmsg-replace="true"></span>
</dd>
</dl>
</fieldset>
<p>This should show the ID of the child field including the parent path: Child_ChildField</p>
<fieldset>
<legend>This is from a form-level partial against a child model</legend>
<dl>
<dt><label for="Child_SomeEnum">Some enum</label> <em class="required">*</em></dt>
<dd>
<select data-val="true" data-val-required="The Some enum field is required." id="Child_SomeEnum" name="Child.SomeEnum"><option selected="selected" value="Value1">Value 1</option>
<option value="ValueWithDescription">Friendly name</option>
<option value="SomeOtherValue">Some other value</option>
</select> <span class="field-validation-valid" data-valmsg-for="Child.SomeEnum" data-valmsg-replace="true"></span>
</dd>
</dl>
</fieldset>
<fieldset>
<legend>This is from a form-level partial against a child model in the base class</legend>
<dl>
<dt><label for="BaseChild_BaseChildField2">Base child field 2</label></dt>
<dd>
<input id="BaseChild_BaseChildField2" name="BaseChild.BaseChildField2" type="text" value=""> <span class="field-validation-valid" data-valmsg-for="BaseChild.BaseChildField2" data-valmsg-replace="true"></span>
</dd>
</dl>
</fieldset>
<fieldset>
<legend>This is in the parent view</legend>
<dl>
<dt><label for="Decimal">Decimal</label> <em class="required">*</em></dt>
<dd>
<input data-val="true" data-val-number="The field Decimal must be a number." data-val-required="The Decimal field is required." id="Decimal" name="Decimal" type="text" value="1.2300">in parent view <span class="field-validation-valid" data-valmsg-for="Decimal" data-valmsg-replace="true"></span>
</dd>
<dt><label for="TextAreaField">Text area field</label></dt>
<dd>
<textarea cols="20" id="TextAreaField" name="TextAreaField" rows="2">Initial value</textarea>from partial against top-level model <span class="field-validation-valid" data-valmsg-for="TextAreaField" data-valmsg-replace="true"></span>
</dd>
<dt><label for="BaseField">Base field</label></dt>
<dd>
<input id="BaseField" name="BaseField" type="text" value="">from partial against top-level model casted to base class <span class="field-validation-valid" data-valmsg-for="BaseField" data-valmsg-replace="true"></span>
</dd>
<dt><label for="ListId">List id</label> <em class="required">*</em></dt>
<dd>
<select data-val="true" data-val-number="The field List id must be a number." data-val-required="The List id field is required." id="ListId" name="ListId"><option value="1">A</option>
<option value="2">B</option>
</select>in parent view <span class="field-validation-valid" data-valmsg-for="ListId" data-valmsg-replace="true"></span>
</dd>
<p>This should show the ID of the child field including the parent path: Child_ChildField</p>
<dt><label for="Child_ChildField">Child field</label> <em class="required">*</em></dt>
<dd>
<input data-val="true" data-val-required="The Child field field is required." id="Child_ChildField" name="Child.ChildField" type="text" value="">From partial against child model <span class="field-validation-valid" data-valmsg-for="Child.ChildField" data-valmsg-replace="true"></span>
</dd>
<dt><label for="BaseChild_BaseChildField">Base child field</label></dt>
<dd>
<input id="BaseChild_BaseChildField" name="BaseChild.BaseChildField" type="text" value="">From partial against child model in the base class <span class="field-validation-valid" data-valmsg-for="BaseChild.BaseChildField" data-valmsg-replace="true"></span>
</dd>
<dt><label for="SomeCheckbox">Some checkbox</label> <em class="required">*</em></dt>
<dd>
<input data-val="true" data-val-required="The Some checkbox field is required." id="SomeCheckbox" name="SomeCheckbox" type="checkbox" value="true"> <label for="SomeCheckbox">Some checkbox</label> in parent view <span class="field-validation-valid" data-valmsg-for="SomeCheckbox" data-valmsg-replace="true"></span>
</dd>
</dl>
</fieldset>
<div class="form_navigation">
<button type="submit">Submit</button> </div>
</form>