Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
More tests, cleanup etc
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkm committed Nov 30, 2017
1 parent 6ed589f commit c6ea066
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 54 deletions.
14 changes: 10 additions & 4 deletions src/Microsoft.AspNetCore.Mvc.TagHelpers/PartialTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
[HtmlTargetElement("partial", Attributes = "name", TagStructure = TagStructure.WithoutEndTag)]
public class PartialTagHelper : TagHelper
{
private const string ForAttributeName = "asp-for";
private readonly ICompositeViewEngine _viewEngine;
private readonly IViewBufferScope _viewBufferScope;

Expand All @@ -37,9 +38,10 @@ public PartialTagHelper(
public string Name { get; set; }

/// <summary>
/// A model to pass into the partial view.
/// An expression to be evaluated against the current model.
/// </summary>
public object Model { get; set; }
[HtmlAttributeName(ForAttributeName)]
public ModelExpression For { get; set; }

/// <summary>
/// A <see cref="ViewDataDictionary"/> to pass into the partial view.
Expand Down Expand Up @@ -101,11 +103,15 @@ private async Task RenderPartialViewAsync(TextWriter writer)
{
// Determine which ViewData we should use to construct a new ViewData
var baseViewData = ViewData ?? ViewContext.ViewData;
var model = Model ?? ViewContext.ViewData.Model;

var model = For?.Model ?? ViewContext.ViewData.Model;
var newViewData = new ViewDataDictionary<object>(baseViewData, model);
var partialViewContext = new ViewContext(ViewContext, view, newViewData, writer);

if (For?.Name != null)
{
newViewData.TemplateInfo.HtmlFieldPrefix = newViewData.TemplateInfo.GetFullHtmlFieldName(For.Name);
}

await view.RenderAsync(partialViewContext);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ public static TheoryData<string, string> WebPagesData
// Only attribute order should differ.
{ "Order", "/HtmlGeneration_Order/Submit" },
{ "OrderUsingHtmlHelpers", "/HtmlGeneration_Order/Submit" },
// Testing PartialTagHelper
{ "PartialTagHelperWithoutModel", null },
{ "Warehouse", null },
// Testing InputTagHelpers invoked in the partial views
{ "ProductList", "/HtmlGeneration_Product" },
{ "ProductListUsingTagHelpers", "/HtmlGeneration_Product" },
// Testing the ScriptTagHelper
{ "Script", null },
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PartialTagHelperWithoutModel: Hello from partial
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<body>
<form action="/HtmlGeneration_Product" method="post">
<div>
<label class="product" for="z0__product_HomePage">HomePage</label>
<input type="url" size="50" disabled="disabled" readonly="readonly" id="z0__product_HomePage" name="[0].product.HomePage" value="http://www.contoso.com/" />
<label class="product" for="z0__HomePage">HomePage</label>
<input type="url" size="50" disabled="disabled" readonly="readonly" id="z0__HomePage" name="[0].HomePage" value="http://www.contoso.com/" />
</div>

<div>
Expand All @@ -23,8 +23,8 @@
</textarea>
</div>
<div>
<label class="product" for="z1__product_HomePage">HomePage</label>
<input type="url" size="50" disabled="disabled" readonly="readonly" id="z1__product_HomePage" name="[1].product.HomePage" value="" />
<label class="product" for="z1__HomePage">HomePage</label>
<input type="url" size="50" disabled="disabled" readonly="readonly" id="z1__HomePage" name="[1].HomePage" value="" />
</div>

<div>
Expand All @@ -41,8 +41,8 @@
</textarea>
</div>
<div>
<label class="product" for="z2__product_HomePage">HomePage</label>
<input type="url" size="50" disabled="disabled" readonly="readonly" id="z2__product_HomePage" name="[2].product.HomePage" value="" />
<label class="product" for="z2__HomePage">HomePage</label>
<input type="url" size="50" disabled="disabled" readonly="readonly" id="z2__HomePage" name="[2].HomePage" value="" />
</div>

<div>
Expand All @@ -58,7 +58,9 @@
<textarea rows="4" cols="50" class="product" id="z2__Description" name="[2].Description">
Product_2 description</textarea>
</div>

<div>HtmlFieldPrefix = </div>
<input type="submit" />
<input name="__RequestVerificationToken" type="hidden" value="{0}" /></form>
</body>
</html>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h3>City_1</h3>

<div>
<label for="Employee_Name">Name</label>
<input type="text" id="Employee_Name" name="Employee.Name" value="EmployeeName_1" />
</div>
<div>
<label for="Employee_OfficeNumber">OfficeNumber</label>
<input type="number" id="Employee_OfficeNumber" name="Employee.OfficeNumber" value="Number_1" />
</div>
<div>
<label for="Employee_Address">Address</label>
<textarea rows="4" cols="50" id="Employee_Address" name="Employee.Address">
Address_1</textarea>
</div>
Loading

0 comments on commit c6ea066

Please sign in to comment.