我有一个名为" BookController" :
public ActionResult Book(int? id)
{
var result = db.Books.Where(b => b.Book_id == id).Single();
var user = db.AspNetUsers.Where(b => b.Id == result.User_ID).Single();
ViewBag.User_ID = user.UserName;
return View(result);
}
public ActionResult SameAuthor(string author)
{
var result = db.Books.Where(b => b.Author_name == author).Take(4);
return View(result.ToList());
}
这也是视图代码" books.cshtml" :
@model SmartBookLibrary.Models.Book
@using SmartBookLibrary.Controllers
@{
ViewBag.Title = Model.Book_name;
BookController book = new BookController();
var auth = Model.Author_name;
}
@section AdditionalMeta
{
<link href="~/Content/blog-post.css" rel="stylesheet" />
<link href="~/Content/font-awesome.min.css" rel="stylesheet" />
}
<div class="container">
<div class="row">
<!-- Blog Post Content Column -->
<div class="col-lg-8">
<!-- Blog Post -->
<!-- Title -->
<h1>@Model.Book_name (@Model.Edition th Edition) <a href="~/Admin/books/edit/@Model.Book_id"><small class="fa fa-edit"></small></a></h1>
<!-- Author -->
<p class="lead">
by <a href="#">@ViewBag.User_ID</a>
</p>
<hr>
<!-- Date/Time -->
<p><span class="glyphicon glyphicon-time"></span> Posted on @Model.Publish_date</p>
<hr>
<!-- Preview Image -->
<div class="row">
<div class="col-md-3">
<img class="img-show" src="/Books/RetrieveImage/@Model.Book_id" alt="">
</div>
<div class="col-md-6 col-md-offset-1">
<dl>Book Name : @Model.Book_name</dl>
<dl>Book Edition : @Model.Edition</dl>
<dl>Author Name : @Model.Author_name</dl>
<dl>Category : @Model.Category.Category_name</dl>
<dl>Book Name: @Model.Book_name</dl>
<dl>ISN : 2015</dl>
</div>
</div>
<hr>
<!-- Post Content -->
<p class="lead">@Html.Raw(Model.Book_Description)</p>
<button class="btn btn-block btn-lg btn-success">
Download @Model.Book_name
<i class="fa fa-cloud-download"></i>
</button>
<hr>
<div class="well">
<div class="row">
@* I Want to call the contoller method , it will return 4 post , then i want to show them here *@
@{
var b = book.SameAuthor(auth);
foreach (var cat in b)
{
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="/Books/RetrieveImage/" alt="Generic placeholder thumbnail">
</div>
<div class="caption">
<h3>Book_name</h3>
<p>Some sample text. Some sample text.</p>
<p>
<a href="#" class="btn btn-primary" role="button">
Button
</a>
<a href="#" class="btn btn-default" role="button">
Button
</a>
</p>
</div>
</div>
}
}
</div>
</div>
<hr>
<!-- Blog Comments -->
<!-- Comments Form -->
<div class="well">
<h4>Leave a Comment:</h4>
<form role="form">
<div class="form-group">
<textarea class="form-control" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<hr>
<!-- Posted Comments -->
<!-- Comment -->
<div class="media">
<a class="pull-left" href="#">
<img class="media-object" src="http://placehold.it/64x64" alt="">
</a>
<div class="media-body">
<h4 class="media-heading">
Start Bootstrap
<small>August 25, 2014 at 9:30 PM</small>
</h4>
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
</div>
</div>
<!-- Comment -->
<div class="media">
<a class="pull-left" href="#">
<img class="media-object" src="http://placehold.it/64x64" alt="">
</a>
<div class="media-body">
<h4 class="media-heading">
Start Bootstrap
<small>August 25, 2014 at 9:30 PM</small>
</h4>
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
<!-- Nested Comment -->
<div class="media">
<a class="pull-left" href="#">
<img class="media-object" src="http://placehold.it/64x64" alt="">
</a>
<div class="media-body">
<h4 class="media-heading">
Nested Start Bootstrap
<small>August 25, 2014 at 9:30 PM</small>
</h4>
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
</div>
</div>
<!-- End Nested Comment -->
</div>
</div>
</div>
<!-- Blog Sidebar Widgets Column -->
<div class="col-md-4">
<!-- Blog Search Well -->
<div class="well">
<h4>Search</h4>
<div class="input-group">
<input type="text" class="form-control">
<span class="input-group-btn">
<button class="btn btn-default" type="button">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
<!-- /.input-group -->
</div>
<!-- Blog Categories Well -->
<div class="well">
<h4>Blog Categories</h4>
<div class="row">
<div class="col-lg-6">
<ul class="list-unstyled">
<li>
<a href="#">Category Name</a>
</li>
<li>
<a href="#">Category Name</a>
</li>
<li>
<a href="#">Category Name</a>
</li>
<li>
<a href="#">Category Name</a>
</li>
</ul>
</div>
<div class="col-lg-6">
<ul class="list-unstyled">
<li>
<a href="#">Category Name</a>
</li>
<li>
<a href="#">Category Name</a>
</li>
<li>
<a href="#">Category Name</a>
</li>
<li>
<a href="#">Category Name</a>
</li>
</ul>
</div>
</div>
<!-- /.row -->
</div>
<!-- Side Widget Well -->
<div class="well">
<h4>Books May You Interest</h4>
</div>
</div>
</div>
<!-- /.row -->
</div>
我的问题实际上在这个代码&#34;在books.cshtml&#34;:
@{
var b = book.SameAuthor(auth);
foreach (var cat in b)
{
<div class="col-sm-6 col-md-3">
<div class="thumbnail">
<img src="/Books/RetrieveImage/" alt="Generic placeholder thumbnail">
</div>
<div class="caption">
<h3>Book_name</h3>
<p>Some sample text. Some sample text.</p>
<p>
<a href="#" class="btn btn-primary" role="button">
Button
</a>
<a href="#" class="btn btn-default" role="button">
Button
</a>
</p>
</div>
</div>
}
}
我得到的错误是:
Severity Code Description Project File Line Suppression State
Error CS1579 foreach statement cannot operate on variables of type 'System.Web.Mvc.ActionResult' because 'System.Web.Mvc.ActionResult' does not contain a public definition for 'GetEnumerator' SmartBookLibrary C:\Users\Firas\documents\visual studio 2015\Projects\SmartBookLibrary\SmartBookLibrary\Views\Book\Book.cshtml 73 Active
注意:我试图将模型类型更改为:
@model IEnumerable <SmartBookLibrary.Models.Book>
并且错误消失了,但我得到了所有视图代码的错误(任何代码都以@model开头),这是我得到的错误示例:
Severity Code Description Project File Line Suppression State
Error CS1061 'IEnumerable<Book>' does not contain a definition for 'Book_name' and no extension method 'Book_name' accepting a first argument of type 'IEnumerable<Book>' could be found (are you missing a using directive or an assembly reference?) SmartBookLibrary C:\Users\Firas\documents\visual studio 2015\Projects\SmartBookLibrary\SmartBookLibrary\Views\Book\Book.cshtml 4 Active
任何人都可以帮我解决问题!并谢谢
答案 0 :(得分:1)
错误是不言自明的。 class X {
int x1, x2, x3;
}
操作方法返回SameAuthor()
类型。你不能循环使用它!
看起来您的ActionResult
操作方法会返回包含所需数据的视图(标记)。那么为什么不在主视图中使用Html.Action帮助方法
SameAuthor
或您应该为视图创建一个视图模型,其中包含一本书的详细信息以及“同一作者的书籍”的书籍集合。
@Html.Action("SameAuthor", Model.Author_name)
并在您的GET操作中加载此数据
public class BooksDetailsVm
{
public Book Book { set;get;}
public List<Book> BooksFromSameAuthor { set;get;}
}
您的观点,这是我们新的BookDetailsVm
的强类型public ActionResult Book(int? id)
{
if (id != null)
{
var vm = new BooksDetailsVm();
var b = db.Books.Where(O => O.Book_id == id).Single();
vm.Book = b;
var user = db.AspNetUsers.Where(P => P.Id == b.User_ID).Single();
vm.BooksFromSameAuthor = db.Books.Where(t => t.Author_name == b.Author_name).Take(4).ToList();
ViewBag.User_ID = user.UserName;
return View(vm);
}
return HttpNotFound();
}