-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from MRCollective/partialfor
@f.PartialFor, @f.Partial, @s.PartialFor, @s.Partial
- Loading branch information
Showing
23 changed files
with
662 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...ed_via_form_or_section_and_when_used_for_top_level_property_or_sub_property.approved.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
Partials.cshtml | ||
|
||
@model ViewModelExample | ||
|
||
@{ | ||
ViewBag.Title = "Partials"; | ||
} | ||
|
||
<h1>Partials</h1> | ||
|
||
@using (var f = Html.BeginChameleonForm()) | ||
{ | ||
@f.Partial("_ParentPartial") | ||
@f.PartialFor(m => m.Child, "_ChildPartial") | ||
|
||
using (var s = f.BeginSection("This is in the parent view")) | ||
{ | ||
@s.FieldFor(m => m.Decimal).Append("in parent view") | ||
@s.Partial("_ParentPartial") | ||
@s.FieldFor(m => m.ListId).Append("in parent view") | ||
@s.PartialFor(m => m.Child, "_ChildPartial") | ||
@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 | ||
|
||
@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) | ||
} | ||
} | ||
|
||
===== | ||
|
||
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 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 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="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> | ||
<dt><label for="Child_ChildField">Child field</label></dt> | ||
<dd> | ||
<input 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="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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.CodeDom; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Text.RegularExpressions; | ||
using ApprovalTests.Html; | ||
using ApprovalTests.Reporters; | ||
using NUnit.Framework; | ||
using OpenQA.Selenium; | ||
using OpenQA.Selenium.Support.UI; | ||
|
||
namespace ChameleonForms.AcceptanceTests | ||
{ | ||
/// <summary> | ||
/// Loading partial views is very difficult to test by unit testing. | ||
/// </summary> | ||
[UseReporter(typeof(DiffReporter))] | ||
public class PartialForTests | ||
{ | ||
[Test] | ||
public void Should_render_correctly_when_used_via_form_or_section_and_when_used_for_top_level_property_or_sub_property() | ||
{ | ||
var renderedSource = GetRenederedSource("/ExampleForms/Partials"); | ||
HtmlApprovals.VerifyHtml(string.Format("Partials.cshtml\r\n\r\n{0}\r\n=====\r\n\r\n_ParentPartial.cshtml\r\n\r\n{1}\r\n=====\r\n\r\n_ChildPartial.cshtml\r\n\r\n{2}\r\n=====\r\n\r\nRendered Source\r\n\r\n{3}", | ||
GetViewContents("Partials"), | ||
GetViewContents("_ParentPartial"), | ||
GetViewContents("_ChildPartial"), | ||
renderedSource)); | ||
} | ||
|
||
private string GetRenederedSource(string url) | ||
{ | ||
Host.Instance.Application.Browser.Navigate().GoToUrl(string.Format("http://localhost:12345{0}", url)); | ||
new WebDriverWait(Host.Instance.Application.Browser, TimeSpan.FromSeconds(5)) | ||
.Until(b => b.FindElement(By.Id("Int"))); | ||
var renderedSource = Host.Instance.Application.Browser.PageSource; | ||
var getFormContent = new Regex(@".*?(<form(.|\n|\r)+?<\/form>).*", RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); | ||
return getFormContent.Match(renderedSource).Groups[1].Value; | ||
} | ||
|
||
private string GetViewContents(string viewPath) | ||
{ | ||
return File.ReadAllText(string.Format(ViewPath, viewPath)); | ||
} | ||
|
||
private static readonly string ViewPath = Path.Combine(Path.GetDirectoryName(typeof(PartialForTests).Assembly.CodeBase.Replace("file:///", "")), "..", "..", "..", "ChameleonForms.Example", "Views", "ExampleForms", "{0}.cshtml"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
@model ViewModelExample | ||
|
||
@{ | ||
ViewBag.Title = "Partials"; | ||
} | ||
|
||
<h1>Partials</h1> | ||
|
||
@using (var f = Html.BeginChameleonForm()) | ||
{ | ||
@f.Partial("_ParentPartial") | ||
@f.PartialFor(m => m.Child, "_ChildPartial") | ||
|
||
using (var s = f.BeginSection("This is in the parent view")) | ||
{ | ||
@s.FieldFor(m => m.Decimal).Append("in parent view") | ||
@s.Partial("_ParentPartial") | ||
@s.FieldFor(m => m.ListId).Append("in parent view") | ||
@s.PartialFor(m => m.Child, "_ChildPartial") | ||
@s.FieldFor(m => m.SomeCheckbox).Append(" in parent view") | ||
} | ||
|
||
using (var n = f.BeginNavigation()) | ||
{ | ||
@n.Submit("Submit") | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
ChameleonForms.Example/Views/ExampleForms/_ChildPartial.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@model ChildViewModel | ||
|
||
@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) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
ChameleonForms.Example/Views/ExampleForms/_ParentPartial.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.