从模型渲染视图 [英] Rendering view from a model

查看:106
本文介绍了从模型渲染视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i需要这个支持,我现在关注udemy的视频,因为我在MVC中的开发,我遇到的问题是跟随视频并且来自培训师的指令一直很好,但我有一个问题,即通过模型从控制器渲染视图。下面是我得到的错误和文件中的代码



1.没有@model IEnunerator的剃刀视图< vidly.models.customers>



代码:

hello all,

i need this support, i am presently following a video on udemy for my development in MVC, the issue i am having is that following the video and the instruction from the trainer has been nice but i have an issue of rendering the view from controller via the model. below is the error i get and codes in the files

1. Razor view without the @model IEnunerator<vidly.models.customers>

code:

Quote:

@model IEnumerable< vidly.models.customer> ;



@ *

注意:我已将此视图的模型设置为IEnumerable< customer> ;.

这是list类实现的简单接口。由于这个视图中的
我们只想迭代这个列表,而且我们不需要
需要List类中的任何操作(例如Add,Remove等) ),

最好使用IEnumerable接口,它允许使用

迭代列表。如果将来我们用一个

不同的数据结构替换List,只要它是可枚举的,我们的视图代码

将保持不变。

* @



@ {

ViewBag.Title =客户;

布局= 〜/ Views / Shared / _Layout.cshtml;

}



@ *客户数量为:@ Model.Count()* @



客户



@if(!Model.Any())

{

我们还没有任何客户。

@model IEnumerable<vidly.models.customer>

@*
Note: I've set the model for this view to IEnumerable<customer>.
This is a simple interface implemented by the list class. Since
in this view we only want to iterate over this list, and we don't
need any of the operations in the List class (eg Add, Remove, etc),
it's better to use the IEnumerable interface, which allows use to
iterate over the list. If in the future, we replace the List with a
different data structure, as long as it is enumerable, our view code
will remain unchanged.
*@

@{
ViewBag.Title = "Customers";
Layout = "~/Views/Shared/_Layout.cshtml";
}

@*Customer count is: @Model.Count()*@

Customers


@if (!Model.Any())
{

We don't have any customers yet.





错误



Error

Quote:

等待操作超时

描述:执行当前网页时发生未处理的异常请求。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.ComponentModel.Win32Exception:等待操作超时



源错误:< br $>




第21行:public ViewResult Index()

第22行:{

第23行:var customers = _context.Customers.ToList();

第24行:

第25行:返回查看(客户);

The wait operation timed out
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ComponentModel.Win32Exception: The wait operation timed out

Source Error:


Line 21: public ViewResult Index()
Line 22: {
Line 23: var customers = _context.Customers.ToList();
Line 24:
Line 25: return View(customers);







2.使用@model进行Razor视图IEnumerable< vidly.models.customer>



代码:

@ *

注意:我已将此视图的模型设置为IEnumerable< customer> ;.

这是list类实现的简单接口。由于这个视图中的
我们只想迭代这个列表,而且我们不需要
需要List类中的任何操作(例如Add,Remove等) ),

最好使用IEnumerable接口,它允许使用

迭代列表。如果将来我们用一个

不同的数据结构替换List,只要它是可枚举的,我们的视图代码

将保持不变。

* @



@ {

ViewBag.Title =客户;

布局= 〜/ Views / Shared / _Layout.cshtml;

}



@ *客户数量为:@ Model.Count()* @



客户



@if(!Model.Any())

{

我们还没有任何客户。




2. Razor view with the @model IEnumerable<vidly.models.customer>

Code:
@*
Note: I've set the model for this view to IEnumerable<customer>.
This is a simple interface implemented by the list class. Since
in this view we only want to iterate over this list, and we don't
need any of the operations in the List class (eg Add, Remove, etc),
it's better to use the IEnumerable interface, which allows use to
iterate over the list. If in the future, we replace the List with a
different data structure, as long as it is enumerable, our view code
will remain unchanged.
*@

@{
ViewBag.Title = "Customers";
Layout = "~/Views/Shared/_Layout.cshtml";
}

@*Customer count is: @Model.Count()*@

Customers


@if (!Model.Any())
{

We don't have any customers yet.

引用:

'/'应用程序中的服务器错误。

模型项目传入dictionary的类型为'System.Collections.Generic.List`1 [Vidly.Models.Customer]',但此字典需要一个类型为'Vidly.ViewModels.RandomMovieViewModel'的模型项。

Server Error in '/' Application.
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Vidly.Models.Customer]', but this dictionary requires a model item of type 'Vidly.ViewModels.RandomMovieViewModel'.





CustomerController:





CustomerController:

Quote:

使用System.Collections.Generic;

使用System.Linq;

使用System.Web.Mvc;

使用Vidly.Models;



名称空间Vidly.Controllers

{

公共类CustomersController:Controller

{

private ApplicationDbContext _context;



public CustomersController()

{

_context =新的ApplicationD bContext();

}



protected override void Dispose(bool disposing)

{

_context.Dispose();

}

public ViewResult Index()

{

var customers = _context.Customers.ToList();



返回查看(客户);

}



public ActionResult Details(int id)

{

var customer = _context.Customers.SingleOrDefault(c => c.Id == id);



if(customer == null)

返回HttpNotFound();



返回查看(客户);

}



}

}

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Vidly.Models;

namespace Vidly.Controllers
{
public class CustomersController : Controller
{
private ApplicationDbContext _context;

public CustomersController()
{
_context = new ApplicationDbContext();
}

protected override void Dispose(bool disposing)
{
_context.Dispose();
}
public ViewResult Index()
{
var customers = _context.Customers.ToList();

return View(customers);
}

public ActionResult Details(int id)
{
var customer = _context.Customers.SingleOrDefault(c => c.Id == id);

if (customer == null)
return HttpNotFound();

return View(customer);
}

}
}





客户型号:





Customer Model:

引用:

使用System;

使用System.Collections.Generic;

使用System.ComponentModel.DataAnnotations;

使用System.Linq;

使用System.Web;



名称空间Vidly.Models

{

公共类客户

{

public int Id {get;组; }

[必填]

[StringLength(255)]

public string Name {get;组; }

public bool IsSubscribedToNewsletter {get;组; }

public MembershipType MembershipType {get;组; } $ / $
公共字节MembershipTypeId {get;组; }



}

}

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Vidly.Models
{
public class Customer
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
public bool IsSubscribedToNewsletter { get; set; }
public MembershipType MembershipType { get; set; }
public byte MembershipTypeId { get; set; }

}
}





我尝试了什么:



我已经检查了GIT和谷歌但是所有推荐的修复都没有用,所以我想知道这个问题是关联的使用或不使用的VS版本,更多我正在使用VS2015



What I have tried:

I have checked on GIT and google but all the recommended fix has not worked, so i want to know the issue is associated with the VS version i am using or not, more so i am using VS2015

推荐答案

错误意味着它所说的。 DB正在超时。你的代码很好
Error means what it says. The DB is timing out. Your code is fine


这篇关于从模型渲染视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆