ASP.NET MVC:将对象列表从Controller传递到View

时间:2018-12-15 01:55:28

标签: asp.net asp.net-mvc view controller

所以这段代码是我的Controller类。我正在将书籍清单发送到View

public class BooksController : Controller
{
     public ActionResult Index(int page = 0)
     {
          List<Book> data = BookRepository.GetInstance().getAllBooks();
          return this.View(data);
     }
}

所以我在顶部引用模型类

@model BookStore.Models.Book

当我尝试像下面的代码那样迭代时,它说它不包含GetEnumerator的公共实例,但是我返回了一个对象列表,如何在for循环中访问列表中的每个对象?

<ul>
   @foreach(var book in Model)
   {

   }
</ul>

2 个答案:

答案 0 :(得分:2)

问题在以下行:

@model BookStore.Models.Book

您正在将List<Book>从控制器传递到视图,但是视图的模型类型为Book。因此,将上面的行编写如下:

@model List<BookStore.Models.Book>

答案 1 :(得分:1)

您正在从控制器传递对象列表以进行查看,因此必须在您的关键字中使用 IEnumerable List IList 视图的。因此,您可以使用以下方式。

frame

OR

class ViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    let image = UIImage(named: "Miho_Small.png")

    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 250, height: 250))
    imageView.contentMode = .scaleAspectFit
    imageView.image = image
    imageView.backgroundColor = nil

    let backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 250))
    backgroundView.translatesAutoresizingMaskIntoConstraints = false

    let gradient = CAGradientLayer()
    gradient.frame = backgroundView.bounds
    let startColor = UIColor(red: 30/255, green: 113/255, blue: 79/255, alpha: 0).cgColor
    let endColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1).cgColor
    gradient.colors = [startColor, endColor]
    backgroundView.layer.insertSublayer(gradient, at: 0)

    backgroundView.addSubview(imageView)

    view.addSubview(backgroundView)

    imageView.leadingAnchor.constraint(equalTo: backgroundView.leadingAnchor).isActive = true
    imageView.trailingAnchor.constraint(equalTo: backgroundView.trailingAnchor).isActive = true
    imageView.topAnchor.constraint(equalTo: backgroundView.topAnchor).isActive = true
    imageView.bottomAnchor.constraint(equalTo: backgroundView.bottomAnchor).isActive = true

    backgroundView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    backgroundView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

    backgroundView.addConstraint(NSLayoutConstraint(item: backgroundView,
                                                    attribute: .width,
                                                    relatedBy: .equal,
                                                    toItem: nil,
                                                    attribute: .notAnAttribute,
                                                    multiplier: 1.0,
                                                    constant: 250))
    backgroundView.addConstraint(NSLayoutConstraint(item: backgroundView,
                                                    attribute: .width,
                                                    relatedBy: .equal,
                                                    toItem: backgroundView,
                                                    attribute: .height,
                                                    multiplier: 1.0,
                                                    constant: 0))


  }


}

OR

@model IEnumerable<BookStore.Models.Book>

我希望它能起作用。