5
大家誰能幫我...林很新的C#和MVC如何處理一個列表,集合或IEnumerable的在查看
的型號是:
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>
}
,我不知道哪裏出了問題?
它顯示了一個錯誤,而渲染視圖..
我也建議[創建一個視圖模型具體到一個封裝視圖的需要,是可擴展的視圖(HTTP ://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvc3fundamentals_topic7.aspx)。 –