4

Hi all can anyone help me ... Im very new to c# and mvc

The Model is:

public class CatagoryModel {
    public int Id { get; set; }
    public string  CName { get; set; }
}

The Controller is:

   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);
      }

The View like this

@model MvcApplication1.Models.CatagoryModel
<ul>
@foreach(var k in Model){
   <li>@k.Id</li> 
    <li>@k.CName</li>

}

And i dont know what went wrong ...
It shows an error while rendering the view ..

4

2 回答 2

5

好吧,如果您的模型是Enumerable,在您看来,它必须是可枚举的!

@model IEnumerable<MvcApplication1.Models.CatagoryModel>
于 2013-07-16T11:28:16.427 回答
0

您的模型是一个列表CategoryModel,而不是一个CategoryModel

@model IEnumerable<MvcApplication1.Models.CatagoryModel>
于 2013-07-16T11:29:34.633 回答