-
Notifications
You must be signed in to change notification settings - Fork 56
/
RandomComponent.cs
65 lines (56 loc) · 2.2 KB
/
RandomComponent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System.IO;
using System.Text.Encodings.Web;
using ChameleonForms.Component;
using ChameleonForms.Enums;
using ChameleonForms.Example.Forms.Templates;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
namespace ChameleonForms.Example.Forms.Components
{
/// <summary>
/// Illustrates using a custom template class and wrapping an unencoded string output.
/// </summary>
public class RandomComponent<TModel> : IFormComponent<TModel>
{
public IForm<TModel> Form { get; private set; }
public RandomComponent(IForm<TModel> form)
{
Form = form;
}
public override string ToString()
{
return ((RandomFormTemplate)Form.Template).RandomComponent();
}
}
/// <summary>
/// Illustrates using a custom template class and wrapping an encoded HTML string output.
/// </summary>
public class RandomComponent2<TModel> : IFormComponent<TModel>, IHtmlContent
{
public IForm<TModel> Form { get; private set; }
public RandomComponent2(IForm<TModel> form)
{
Form = form;
}
public void WriteTo(TextWriter writer, HtmlEncoder encoder)
{
((RandomFormTemplate)Form.Template).RandomComponent2().WriteTo(writer, encoder);
}
}
public static class RandomComponentExtensions
{
public static Form<TModel> BeginRandomForm<TModel>(this IHtmlHelper<TModel> helper, string action, FormMethod method, object htmlAttributes = null, EncType? enctype = null, bool? outputAntiforgeryToken = null)
{
return new Form<TModel>(helper, new RandomFormTemplate(), action, method, htmlAttributes.ToHtmlAttributes(), enctype, outputAntiforgeryToken);
}
public static RandomComponent<TModel> RandomComponent<TModel>(this Form<TModel> form)
{
return new RandomComponent<TModel>(form);
}
public static RandomComponent2<TModel> RandomComponent2<TModel>(this Form<TModel> form)
{
return new RandomComponent2<TModel>(form);
}
}
}