简体   繁体   English

如何通过使用POST /表单叶模板传递数据?

[英]How to pass data from using POST/form leaf template?

I have a couple of major gaps in my understanding of vapor/leaf/html. 我对vapor / leaf / html的理解有两个主要空白。 I am working from the "todo" example that is created using the beta branch of vapor. 我正在使用使用蒸气的beta分支创建的“ todo”示例进行工作。

First, I made my own fluent model (no problems that I know of): 首先,我建立了自己的流利模型(我没有发现任何问题):

import FluentSQLite
import Vapor
final class affordatmodel: SQLiteModel {
    var id: Int?
    var propertyCost: String
    var targetEquity: String
    var interestRate: String
    var amortization: String
    var sponsor1: String
    var sponsor2: String
    var rent: String
    var rentInflation: String
    var propertyTaxes: String
    var propertyTaxesInflation: String
    var strataFees: String
    var strataFeesInflation: String
    init(propertyCost: String, targetEquity: String, interestRate: String, amortization: String, sponsor1: String, sponsor2: String, rent: String, rentInflation: String, propertyTaxes: String, propertyTaxesInflation: String, strataFees: String, strataFeesInflation: String) {
        self.propertyCost = propertyCost
        self.targetEquity = targetEquity
        self.interestRate = interestRate
        self.amortization = amortization
        self.sponsor1 = sponsor1
        self.sponsor2 = sponsor2
        self.rent = rent
        self.rentInflation = rentInflation
        self.propertyTaxes = propertyTaxes
        self.propertyTaxesInflation = propertyTaxesInflation
        self.strataFees = strataFees
        self.strataFeesInflation = strataFeesInflation
    }
}
/// Allows to be used as a dynamic migration.
extension affordatmodel: Migration { }
/// Allows to be encoded to and decoded from HTTP messages.
extension affordatmodel: Content { }
/// Allows to be used as a dynamic parameter in route definitions.
extension affordatmodel: Parameter { }

Then I make an instance and send it to a leaf template: 然后,我创建一个实例并将其发送到叶子模板:

let defaultData = affordatmodel(propertyCost: "700000", targetEquity: "300000", interestRate: "1", amortization: "20", sponsor1: "500000", sponsor2: "200000", rent: "1200", rentInflation: "1", propertyTaxes: "8000", propertyTaxesInflation: "1", strataFees: "0", strataFeesInflation: "0")
return leaf.render("welcome", ["affordat": defaultData])

And my Leaf template successfully populates the html with the default data (body shown here): 我的Leaf模板使用默认数据(此处显示的正文)成功填充了html:

<body class="container">
    <h1>Payment and Principal Calculations</h1>

    <form action="/affordat" method="POST">
        <div class="form-group">
            <label for="propertyCost">Property Cost</label>
            <input type="number" class="form-control" name="propertyCost" placeholder="#(affordat.propertyCost)">
                </div>
        <div class="form-group">
            <label for="targetEquity">Target Equity</label>
            <input type="number" class="form-control" name="targetEquity" placeholder="#(affordat.targetEquity)">
                </div>
        <div class="form-group">
            <label for="interestRate">Interest Rate</label>
            <input type="number" class="form-control" name="interestRate" placeholder="#(affordat.interestRate)">
                </div>
        <div class="form-group">
            <label for="amortization">Amortization (years)</label>
            <input type="number" class="form-control" name="amortization" placeholder="#(affordat.amortization)">
                </div>
        <div class="form-group">
            <label for="sponsor1">Sponsor 1 Funds</label>
            <input type="number" class="form-control" name="sponsor1" placeholder="#(affordat.sponsor1)">
                </div>
        <div class="form-group">
            <label for="sponsor2">Sponsor 2 Funds</label>
            <input type="number" class="form-control" name="sponsor2" placeholder="#(affordat.sponsor2)">
                </div>
        <div class="form-group">
            <label for="rent">Rent</label>
            <input type="number" class="form-control" name="rent" placeholder="#(affordat.rent)">
                </div>
        <div class="form-group">
            <label for="rentInflation">Rent Inflation (will be used exactly)</label>
            <input type="number" class="form-control" name="rentInflation" placeholder="#(affordat.rentInflation)">
                </div>
        <div class="form-group">
            <label for="propertyTaxes">Property Taxes (first year est.)</label>
            <input type="number" class="form-control" name="propertyTaxes" placeholder="#(affordat.propertyTaxes)">
                </div>
        <div class="form-group">
            <label for="propertyTaxesInflation">Property Taxes Inflation (est.)</label>
            <input type="number" class="form-control" name="propertyTaxesInflation" placeholder="#(affordat.propertyTaxesInflation)">
                </div>
        <div class="form-group">
            <label for="strataFees">Strata Fees (first year est.)</label>
            <input type="number" class="form-control" name="strataFees" placeholder="#(affordat.strataFees)">
                </div>
        <div class="form-group">
            <label for="strataFeesInflation">Strata Fees Inflation (est.)</label>
            <input type="number" class="form-control" name="strataFeesInflation" placeholder="#(affordat.strataFeesInflation)">
                </div>

        <input type="hidden" name="_csrf" value="{{csrfToken}}">
            <button type="submit" class="btn btn-primary">Refresh Calculations</button>
            </form>

</body>

Great, so I know how to get fluent data to HTML. 太好了,所以我知道如何将流畅的数据转换为HTML。 My problem is I don't know how to get it back. 我的问题是我不知道该如何找回。 When the "Post" occurs, the data does not seem to get passed to the controller. 当发生“发布”时,数据似乎没有传递到控制器。 My route is: 我的路线是:

router.post("affordat", use: affordatController.create)

And the relevant part of my controller looks like this: 我的控制器的相关部分如下所示:

import Vapor
final class AffordatController {
    func create(_ req: Request) throws -> Future<affordatmodel> {
        return try req.content.decode(affordatmodel.self).flatMap(to: affordatmodel.self) { affordatmodel1 in
            return affordatmodel1.save(on: req)
        }
    }
}

Which shows me one of my models, with an ID #, but no data. 这向我显示了我的一个模型,其ID号为#,但没有数据。 And I kind of understand why because I didn't really seem to send the post data to the controller. 而且我有点理解为什么,因为我似乎并没有真正将发布数据发送到控制器。 How I am supposed to send the POST data to the controller? 我应该如何将POST数据发送到控制器? Is the problem in my leaf template, my route, or my controller? 是我的叶子模板,路线或控制器中的问题?

It looks like it should work. 看起来应该可以使用。 You can inspect the data being sent to your server in the network inspector in your browser. 您可以在浏览器的网络检查器中检查发送到服务器的数据。 Make sure you preserve logs and you'll be able to see the POST request and the data sent to the server. 确保保留日志,并且能够看到POST请求和发送到服务器的数据。

If you breakpoint at each point in the request you can see what the data is. 如果您在请求中的每个点都有断点,则可以看到数据是什么。

As an aside, it looks like you're submitting an empty form, so it's just filling everything in as blank strings. 顺便说一句,您似乎正在提交一个空表格,因此它只是将所有内容填充为空白字符串。 Do you mean to use value instead of placeholder in the form inputs? 您是否要在表单输入中使用value而不是placeholder Value will pre-populate the data for the user, placeholder will show the value to the user as a suggestion but won't send the data in the form submission. 值将为用户预先填充数据,占位符将向用户显示该值作为建议,但不会在表单提交中发送数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 蒸汽从 postgres 传递数据到叶子模板 - Vapor pass data from postgres to a leaf template 蒸气3-如何从Leaf模板形式填充模型的数组属性 - Vapor 3 - How to populate array property of model from Leaf template form 如何使用 Vapor 为我的叶模板获取 json 文件以显示数据? - How can I fetch a json file using Vapor for my leaf template to show the data? 如何将字符串数据从 API POST 请求传递到另一个 ViewController - How to pass String data from API POST request to another ViewController 如何在 swift 中的 Api 中传递数据 object - How to pass Data object in Post Api in swift 如何将数组作为表单数据传递给 Postman - How to pass array to Postman as form-data 使用swrevealviewcontroller时如何将数据从oneviewcontroller传递到另一个viewcontroller? - How to pass data from oneviewcontroller to anotherviewcontroller while using swrevealviewcontroller? 如何使用 leaf 中的#extend 和#export 标签扩展 Leaf 中的模板? - How do you extend templates in Leaf using the #extend and #export tags in leaf? 如何在帖子正文中使用RxAlamofire传递json进行发布通话 - How to make Post call using RxAlamofire pass json in post body 如何使用委托将数据从 UIPageViewController 传递给子 ViewController - how to pass data from UIPageViewController to child ViewController using delegates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM