通过form的post调用post的函数,对内容添加后,想跳转到详细页,但是详细页需要id指定get
方法才可以访问,给button添加herf指定id也没用,请问怎么处理的。
<form class="layui-form layui-form-pane" method="post" enctype="multipart/form-data"action="/update?id={{.note.Id}}" >
<div class="layui-form-item">
<label class="layui-form-label">标题</label>
<div class="layui-input-block">
<input type="text" name="title" required=""
value="{{.note.Title}}"
lay-verify="required" placeholder="请输入标题"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item layui-form-text">
<div class="layui-input-block" class="info">
{{/* <div id="edit" style="background: #fff;">*/}}
<textarea name="info" class="info" style="width: 730px;height: 400px;overflow-x:hidden;word-wrap: break-word " >{{.note.Content}}</textarea>
{{/*/</div>*/}}
{{/* <input class="input-xlarge focused" name="info" type="text" id="info" >*/}}
</div>
</div>
<div class="layui-form-item">
<label>上传图片:</label>
<img id="imgarea" src="{{.note.Img}}" width="240"/>
<input type="file" id="inputarea" style="color: transparent" class="input_file" name="uploadname">
</div>
<div class="layui-form-item">
<button class="layui-btn"
lay-submit=""
lay-filter="save" onclick=javascrtpt:jump()>修改
</button>
</div>
</form>
参考beego orm Insert 取到id
```golang
// https://beego.me/docs/mvc/model/object.md#insert
id, err := o.Insert(&user)
if err == nil {
fmt.Println(id)
}
```
第二步执行跳转
```golang
c.Redirect("/detail?id="+strconv.Itoa(id), 302)
```
#3
更多评论
form 提交到Controller, 参数效验通过后,调用Redirect 方法
见源码
```golang
func (ctx *Context) Redirect(status int, localurl string) {
http.Redirect(ctx.ResponseWriter, ctx.Request, localurl, status)
}
```
#1
<a href="/user/yepinf" title="@yepinf">@yepinf</a> 提交到controller后,跳转不到我的要/details, details文章需要id才能get,现在跳转到index可以,
post函数:
func (c*IndexController) HandleMessage() {
o:=orm.NewOrm()
message:=models.Message{}
content:=c.GetString("comment")
message.Content=content
_,err:=o.Insert(&message)
if err!=nil{
beego.Info("插入错误")
}
id,err:=c.GetInt("id")
beego.Info("id is ",id)
if err != nil {
beego.Info("获取文章ID错误",err)
return}
note:=models.Note{Id:id}
m2m := o.QueryM2M(&note,"Messages")
o.Read(&message,"Content")
//多对多插入
m2m.Add(message)
if err != nil {
beego.Info("插入失败",err)
return
}
c.Data["note"]= note
c.Redirect("/index", 302)
}
路由设置:
beego.Router("/details",&controllers.IndexController{},"get:ShowDetail")
beego.Router("/comment",&controllers.IndexController{},"get:ShowComment;post:HandleMessage")
#2