1

I’ve written a code snippet creates HTML form via C#. But I want the form’s fields to be bound class’s field after the form is submitted. How can I do that and check the result(if the class’s fields are filled)? Moreover, I don’t know how to test the code via Postman or Fiddle. Could you exemplify? For example, when the form is filled via a browser, I don’t know how to see the result forwarded to sent.

HTML form,

<form action="sent” method="POST"<br>
    <label for="firstName">First Name Label:</label> 
    <input type="text" title="testTitle" name="firstName" placeholder="First Name" ><br>
    <label for="lastName">Last Name Label:</label> 
    <input type="text" name="lastName" placeholder="Last Name" ><br>
<input type="submit" name = "noname">
</form> 

Nancy,

Get("/form", parameters =>
{
   // codes to construct the above HTML code
   return Response.AsText(form.ToString(), "text/html");
}

// Received form information (fields)
Post("/sent”, _ =>
{
    testClass receivedData = this.Bind<testClass>();
    return new
    {
        success = true,
        message = $"Record recieved First Name = {receivedData.Name}",
        message2 = $"Record recieved Last Name = {receivedData.SurName}"
    };
});

testClass,

public class testClass
{
   public string Name { get; set; }
   public string SurName { get; set; }
}
  • Maybe a personal preference, but I dislike referencing data using HTML `name` attribute, it could lead to duplicated data if form construction goes south. Im not into nancy, but can it use `id` attribute? IMHO it would be more robust HTML unless repeating data should be allowed (tables/grids) construction. – Cleptus Jul 04 '19 at 07:19
  • @bradbury9 Thank you for the information. Yes, of course. Your proposal sounds better. Since I’ve started nowadays HTML and web-side of C# sharp, I don’t know some nuances like that. However, I’m not sure whether the code I’ve written references the class’s fields properly even I use `id` in lieu of `name` attribute. – Soner from The Ottoman Empire Jul 04 '19 at 07:24
  • static properties in your `testClass` sound so weird. Is there any reason? Acording to [nancy model binding doc](https://github.com/NancyFx/Nancy/wiki/Model-binding) looks like you called the correct function, I wonder if properties being `static` is not messing nancy's API. – Cleptus Jul 04 '19 at 07:28
  • @bradbury9 my fault sir, fixed. – Soner from The Ottoman Empire Jul 04 '19 at 07:30
  • Regarding the "How can I do that and check the result(if the class’s fields are filled)?" in the linked doc there is a "A Complete Model-binding Example" that does data validation and error message stuff. – Cleptus Jul 04 '19 at 07:35

1 Answers1

1

Serving your form should be easy with this working example.

        Get("/api/base", _ =>
        {
            return Response.AsFile("content/Base.html", "text/html");
        });

Make sure to add content folder and make files inside copy to output directory when newer.

make sure you close tags properly. Also your form could just call a mapped api on submit like this

<form action="/api/submit" method="POST">
<br>
    <label for="firstName">First Name Label:</label> 
    <input type="text" title="testTitle" id="firstName" placeholder="First Name"><br>
    <label for="lastName">Last Name Label:</label> 
    <input type="text" id="lastName" placeholder="Last Name" ><br>
    <input type="submit" id = "noname">
</form> 

https://stackoverflow.com/a/53192255

Post("/api/submit”, args =>
{
    //this.Request.Body;
    var r = (Response)"";
    testClass receivedData = this.Bind<testClass>();
    r = (Response)$"Record recieved First Name = {receivedData.Name}" 
    + Environment.NewLine +
    $"Record recieved Last Name = {receivedData.SurName}";
    r.StatusCode = HttpStatusCode.OK;
    return r;
});

I Could be wrong, but I think it is because you are using a discard for the input.

BanMe
  • 133
  • 1
  • 8