簡體   English   中英

如何將表格綁定到南希

[英]How to bind form to Nancy

我已經編寫了一個通過C#創建HTML表單的代碼段。 但我希望在表單提交后將表單的字段綁定到類的字段。 我該怎么做並檢查結果(如果該類的字段已填寫)? 而且,我不知道如何通過PostmanFiddle測試代碼。 你能舉例說明嗎? 例如,當通過瀏覽器填寫表單時,我不知道如何查看轉發到sent的結果。

HTML表單

<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> 

南希

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; }
}

通過這個工作示例,可以輕松地為表單提供服務。

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

確保較新時添加內容文件夾並將文件復制到輸出目錄。

確保您正確關閉標簽。 您的表單也可以像這樣在提交時調用映射的api

<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;
});

我可能是錯的,但是我認為這是因為您在輸入中使用了丟棄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM