Creating an Html helper like BeginForm in Nancy with a razor

We use a standalone Nancy server, where I use Razor viewengine to serve web views. I am learning how to create custom HtmlHelpers and was able to get simple working examples. But one thing I would like to do is a helper that works like BeginForm (), which you can use with the using statement in the markup to create output both at the beginning and at the end of the block used.

@using (Html.BeginForm()) { <some html here> } 

All the examples that I found are based on Asp.Net MVC, as in this answer: https://stackoverflow.com/a/212618/
This seems to be different than in the Nancy implementation. Is it possible to do this?

+4
source share
3 answers

Try using the extension method as follows:

 public static class HtmlHelperExtensions { public static BeginFormObject BeginForm(this HtmlHelpers helpers, NancyRazorViewBase view) { return new BeginFormObject("<form method=\"post\">", view); } public class BeginFormObject : IDisposable { private NancyRazorViewBase view; public BeginFormObject(string markup, NancyRazorViewBase view) { this.view = view; view.WriteLiteral(markup); } public void Dispose() { view.WriteLiteral("</form>"); } } } 

This allows you to use the following syntax in a razor:

 @using (Html.BeginForm(this)) { ....other stuff } 
+2
source
0
source

Based on Peter’s Response ,
I just created a repository on GitHub to test this.

This is a simple solution that allows you to combine your own hosting and Razor.

Check what exactly you are looking for:
NancySelfHostRazorTest

Good luck.

0
source

Source: https://habr.com/ru/post/1498589/


All Articles