在IEC4中将IEnumerable模型返回给MVC控制器

时间:2014-03-07 13:49:58

标签: c# asp.net asp.net-mvc json asp.net-mvc-4

我正在尝试将viewmodel绑定到来自MVC中的json结果的数据集。 这就是我所拥有的:

我正在创建json结果的函数:

private string WsUrl = "https://myUrl";
public List<RestCategories> GetCategoryResults(string api, string site)
{         
    List<RestCategories> categories = new List<RestCategories>();
    RestCategories cat;
    var webRequest = (HttpWebRequest)WebRequest.Create(WsUrl);
    webRequest.Method = "GET";
    try
    {
        var webResponse = (HttpWebResponse)webRequest.GetResponse();
        if ((webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 0))
        {
            var reader = new StreamReader(webResponse.GetResponseStream());
            string s = reader.ReadToEnd();
            var arr = JsonConvert.DeserializeObject<JArray>(s);
            int i = 1;               

            foreach (JObject obj in arr)
            {
                cat = new RestCategories();
                cat.DisplayInPrimaryCategoryListing = (bool)obj["DisplayInPrimaryCategoryListing"];
                cat.ID = (int)obj["ID"];
                cat.ItemCount = (int)obj["ItemCount"];
                cat.Name = (string)obj["Name"];
                cat.Order = (int)obj["Order"];
                cat.SubCategoryOf = (string)obj["SubCategoryOf"].ToString(); 
                categories.Add(cat);
            }
            return categories;
        }
    }
    catch
    {
        throw;
    }
    return categories;
}

这就是我对CategoriewViewModel所拥有的:

public class CategoriesViewModel
    {
        public IEnumerable<RestCategories> Categories;
    }

RestCategories定义:

public class RestCategories
    {           
        public int ID { get; set; }
        public int ItemCount { get; set; }
        public string Name { get; set; }            
    }

最后在控制器中我想做这样的事情:

[HttpGet]
public ActionResult Categories()
{            
    MyModel modelApi = new MyModel();
    List<RestCategories> itemsResult = modelApi.GetCategoryResults("test", "test");
    CategoriesViewModel modelCat = new CategoriesViewModel
    {
        //I need some magic here to return ienumerable dataset of RestCategories
        Categories = itemsResult.                                   
    };
    return View(modelCat);
}

现在,正如您所看到的,我想返回IEnumerable类型的模型,因为我将从Web服务调用中获得多个结果。也许我需要使用GetCategoryResults方法返回多个结果?即使我这样做,我怎么能查看控制器中返回的对象来获得我想要的东西?

1 个答案:

答案 0 :(得分:3)

List继承IEnumerable。您应该能够将itemsResult变量更改为IEnumerable并让一切都快乐。

[HttpGet]
public ActionResult Categories()
{            
    MyModel modelApi = new MyModel();
    IEnumerable<RestCategories> itemsResult = modelApi.GetCategoryResults("test", "test");
    CategoriesViewModel modelCat = new CategoriesViewModel
    {
        Categories = itemsResult                                  
    };
    return View(modelCat);
}

作为旁注,我倾向于尽可能使用var,因为它首先避免了这些问题。例如

[HttpGet]
public ActionResult Categories()
{            
    var modelApi = new MyModel();
    var itemsResult = modelApi.GetCategoryResults("test", "test");
    var modelCat = new CategoriesViewModel
    {
        Categories = itemsResult                                
    };
    return View(modelCat);
}

这段代码简单,简洁,如果你将来改变变量的类型,它比强类型更脆弱。最终这是风格和偏好的问题。