How can I pass data from controller to form in go lang?

huangapple go评论94阅读模式
英文:

How can I pass data from controller to form in go lang?

问题

我有一个处理程序/控制器,接收一个HTTP请求。

func UpdateHandler(request *http.Request) {
    ID := mux.Vars(request)["ID"]
    UpdateForm.Save(ID, db)
}

然后我有一个表单,我想处理数据并最终更新它。

type UpdateForm struct {
    ID string `json:"type"`
}

func (UpdateForm) Save(db mongo.Database) {
    id := ID
    repository.Update(id)
}

Go会打印出undefined ID

我该如何确保表单从控制器获取值?

英文:

I have a handler / controller that takes in an http request.

func UpdateHandler(request *http.Request) {
    ID := mux.Vars(request)["ID"]
    UpdateForm.Save(ID,db)
}

Then I have a form that I want to process the data and eventually update it.

type UpdateForm struct {
	ID              string            `json:"type"`
}

func (UpdateForm) Save(db mongo.Database) {
    id := ID
	repository.Update(Id)
}

Go will print out undefined ID

How can I make sure that the form gets the value from the controller?

答案1

得分: 2

您可以使用请求中的数据填充表单。如果您的请求包含一个JSON编码的主体,您可以像这样将其解码到您的表单对象中:

package main

import (
	"encoding/json"
	"net/http"
	"strings"
	"fmt"
)

type UpdateForm struct {
	ID string `json:"type"`
}

func main() {
	req, _ := http.NewRequest(
		"POST",
		"http://example.com",
		strings.NewReader(`{"type": "foo"}`),
	)

	var form *UpdateForm
	json.NewDecoder(req.Body).Decode(&form)
	fmt.Println(form.ID) // 输出: foo
}

或者您可以直接这样实例化它:

func UpdateHandler(request *http.Request) {
    ID := mux.Vars(request)["ID"]
    form := &UpdateForm{ID: ID}
    form.Save()
}
英文:

You can populate your form using data from the request. If your request contains a JSON encoded body than you could decode it into your form object like this:

package main

import (
	"encoding/json"
	"net/http"
	"strings"
	"fmt"
)

type UpdateForm struct {
	ID string `json:"type"`
}

func main() {
	req, _ := http.NewRequest(
		"POST",
		"http://example.com",
		strings.NewReader(`{"type": "foo"}`),
	)

	var form *UpdateForm
	json.NewDecoder(req.Body).Decode(&form)
	fmt.Println(form.ID) // Output: foo
}

Or you can instantiate it directly like this:

func UpdateHandler(request *http.Request) {
    ID := mux.Vars(request)["ID"]
    form := &UpdateForm{ID: ID}
    form.Save()
}

答案2

得分: 1

我认为这与处理程序无关,而是你的代码不一致。这一行代码:

UpdateForm.Save(ID,db)

方法Save()接受两个参数,而原始方法签名只接受一个mongo.Database类型的参数。

以下是我认为你的意图:

type UpdateForm struct {
    ID     string   `json:"type"`
}

func (u UpdateForm) Save(db mongo.Database) {
    id := u.ID
    repository.Update(id)
}

// 在某处创建 UpdateForm 实例
var u = UpdateForm{}

func UpdateHandler(request *http.Request) {
    u.ID = mux.Vars(request)["ID"]
    u.Save(db)
}
英文:

I think it has nothing to do with the handler, but your code isn't consistent. This line

UpdateForm.Save(ID,db)

The method Save() takes two arguments, while the original method signature takes only a single mongo.Database type argument.

Here is what I assume was your intention:

type UpdateForm struct {
    ID     string   `json:"type"`
}

func (u UpdateForm) Save(db mongo.Database) {
    id := u.ID
    repository.Update(id)
}

// UpdateForm instance somewhere
var u = UpdateForm{}

func UpdateHandler(request *http.Request) {
    u.ID := mux.Vars(request)["ID"]
    u.Save(db)
}

huangapple
  • 本文由 发表于 2015年11月14日 08:05:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/33703642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定