小编Sam*_*ech的帖子

MVC5 Html.RenderAction与不同的控制器

我从MVC5开始,并从MVC5入门创建了第一个项目.

现在尝试使用Partial Rendering并在MoviesController中添加一个方法,如下所示

[ChildActionOnly]
public ActionResult PriceRange()
{
   var maxprice = db.Movies.Max(m => m.Price);
   var minprice = db.Movies.Min(m => m.Price);
   ViewBag.MaxPrice = maxprice;
   ViewBag.MinPrice = minprice;
   return PartialView();
}
Run Code Online (Sandbox Code Playgroud)

它将电影集合中的最小和最大价格设置为稍后在视图中显示的ViewBag.我试图在不同的视图上呈现它.

首先,我尝试将其渲染Views/Movies/Index.cshtml如下

@{Html.RenderAction("PriceRange");}
Run Code Online (Sandbox Code Playgroud)

它在那里运行良好,结果显示正确,因为它正在使用MoviesController,方法PriceRange定义的同一个类.

然后我尝试Views/Hello/Index.cshtml使用HelloWorldController以下代码渲染它(此视图正在使用)(首先传递Action名称然后传递Controller名称)

@{Html.RenderAction("PriceRange", "MoviesController");}
Run Code Online (Sandbox Code Playgroud)

这里给出了运行时错误

未找到路径'/ HelloWorld/Index'的控制器或未实现IController.

这是来自Views/Hello/Index.cshtml的完整代码

@{
    ViewBag.Title = "Movie List";
}
<h2>My Movie List</h2>
<p>Hello from our view template</p>
@{Html.RenderAction("PriceRange", "MoviesController");}
Run Code Online (Sandbox Code Playgroud)

我通过Google找到了一些例子,他们以相同的方式调用RenderAction助手,首先传递Action名称然后传递Controller名称.

我无法理解我在这里做错了什么.有人可以指出吗?

asp.net-mvc renderaction

25
推荐指数
1
解决办法
7万
查看次数

将接口{}转换为Golang中的map

我正在尝试创建一个可以接受以下的功能

*struct
[]*struct
map[string]*struct
Run Code Online (Sandbox Code Playgroud)

这里struct可以是任何结构,而不仅仅是特定结构.将接口转换为*struct[]*struct正常工作.但给地图错误.

反映后显示它是map [],但在尝试迭代范围时给出错误.

这是代码

package main

import (
    "fmt"
    "reflect"
)

type Book struct {
    ID     int
    Title  string
    Year   int
}

func process(in interface{}, isSlice bool, isMap bool) {
    v := reflect.ValueOf(in)

    if isSlice {
        for i := 0; i < v.Len(); i++ {
            strct := v.Index(i).Interface()
            //... proccess struct
        }
        return
    }

    if isMap {
        fmt.Printf("Type: %v\n", v)     // map[]
        for _, s := range v {           // Error: cannot …
Run Code Online (Sandbox Code Playgroud)

reflection dictionary interface go

16
推荐指数
3
解决办法
4万
查看次数

Go 语言,使用 sqlx.StructScan 扫描嵌入式结构

我刚刚开始学习 Go 语言。我写了以下简单的程序。

在这里,我试图用所有书籍和相关作者填充结构。

Bookstruct 已嵌入Author结构。

package main
import (
    "fmt"
    "log"
    "time"
    "github.com/jmoiron/sqlx"
    _ "github.com/lib/pq"
)

type Book struct {
    ID      int
    Title   string
    Year    int
    Bauther  Auther `db:"auther"`
}

type Auther struct {
    ID      int
    Name    string
    Dob     time.Time
}

func main() {
    db, err := sqlx.Open("postgres", "host=localhost user=testuser dbname=testdb password=testuser")
    if err != nil {
       log.Fatal("DB Conn error: ", err)
    }

    if err = db.Ping(); err != nil {
        log.Fatal("DB Ping error: ", err) …
Run Code Online (Sandbox Code Playgroud)

database struct go sqlx

3
推荐指数
1
解决办法
5510
查看次数