大家好,任何人都可以帮助我......我是c#和mvc
的新手模型是:
public class CatagoryModel {
public int Id { get; set; }
public string CName { get; set; }
}
控制器是:
public ActionResult catagory() {
var c = new CatagoryModel();
var URL="";
HttpWebRequest req = WebRequest.Create(URL)
as HttpWebRequest;
string result = null;
using (HttpWebResponse resp = req.GetResponse()
as HttpWebResponse)
{
StreamReader reader =
new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}
var categories = JsonConvert.DeserializeObject<List<CatagoryModel>>(result);
return View(categories);
}
查看就像这样
@model MvcApplication1.Models.CatagoryModel
<ul>
@foreach(var k in Model){
<li>@k.Id</li>
<li>@k.CName</li>
}
我不知道出了什么问题...... 它在渲染视图时显示错误..
答案 0 :(得分:5)
好吧,如果您的模型是Enumerable
,在您的视图中,它必须是可枚举的!
@model IEnumerable<MvcApplication1.Models.CatagoryModel>
答案 1 :(得分:0)
您的模型是CategoryModel
的列表,而不是一个CategoryModel
:
@model IEnumerable<MvcApplication1.Models.CatagoryModel>