Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 控制器返回列表对象的类型名,而不是列表中的内容_C#_Asp.net Mvc - Fatal编程技术网

C# 控制器返回列表对象的类型名,而不是列表中的内容

C# 控制器返回列表对象的类型名,而不是列表中的内容,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个简单的代码,通过它我试图从控制器方法返回列表对象并在浏览器上显示它。相反,浏览器将列表的类型显示为: System.Collections.Generic.List`1[System.String] 下面是我的代码: 控制器: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Net; using

我有一个简单的代码,通过它我试图从控制器方法返回列表对象并在浏览器上显示它。相反,浏览器将列表的类型显示为:

System.Collections.Generic.List`1[System.String]
下面是我的代码:

控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.Web.Http;
using System.Web.Script.Serialization;
using MvcApplication2.Models;
using Newtonsoft.Json;

namespace MvcApplication2.Controllers
{
    public class CodesController : Controller
    {
        WebClient myclient = new WebClient();

        public IEnumerable<Codes> Get()
        {
            string data = myclient.DownloadString("URL");
            List<Codes> myobj = (List<Codes>)JsonConvert.DeserializeObject(data, typeof(List<Codes>));
            return myobj;
        }
    }
}

任何人都可以告诉我我在哪里丢失了,我希望测试列表中的代码显示在浏览器上,而不是类型
System.Collections.Generic.List`1[System.String]
。我对MVC非常陌生,是否可以从控制器返回简单列表并在浏览器上呈现,而不是使用视图。

控制器操作并不打算返回POCO对象。您最好返回继承自的类的实例,该类负责以所需的方式在HTML中表示您的实际结果

例如,如果您想要执行一些视图并呈现HTML,您可以使用返回的方法

不清楚您希望如何表示您的集合,但我猜可能是JSON。在这种情况下,您可以使用默认方法,或者返回
ActionResult
的实现

如果返回的内容不是从
ActionResult
继承的,asp.net mvc将尝试将其转换为
string
,并以纯文本形式返回转换后的字符串,而不进行任何修改。这个类的方法负责处理您在操作中返回的内容。进一步的代码将仅与
ActionResult
继承器一起使用,因此如果您不返回其中一个继承器,它将被转换:

/// <summary>Creates the action result.</summary>
/// <returns>The action result object.</returns>
/// <param name="controllerContext">The controller context.</param>
/// <param name="actionDescriptor">The action descriptor.</param>
/// <param name="actionReturnValue">The action return value.</param>
protected virtual ActionResult CreateActionResult(ControllerContext controllerContext, ActionDescriptor actionDescriptor, object actionReturnValue)
{
  if (actionReturnValue == null)
    return new EmptyResult();
  var actionResult = actionReturnValue as ActionResult;
  if (actionResult == null)
  {
    actionResult = new ContentResult()
    {
      Content = Convert.ToString(actionReturnValue, (IFormatProvider) CultureInfo.InvariantCulture)
    };
  }
  return actionResult;
}
第一个方法将返回application/json:
[“AA”、“BB”、“CC”]

第二种方法将返回文本/纯文本:
AA、BB、CC

第三个方法将返回text/html,但您必须创建名为
GetListView.cshtml
的视图:

@using System.Collections.Generic.List<string>
<!DOCTYPE html>
<html>
<head>
    <title>page list</title>
</head>
<body>
    @foreach(var item in this.Model)
    {
        <p>@item</p>
    }
</body>
</html>
或者,您可以创建自己的
ActionResult

public class CodesResult : ActionResult
{
    private readonly List<Codes> _list;

    public CodesResult(List<Codes> list)
    {
        this._list = list;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");

        var response = context.HttpContext.Response;

        response.ContentType = "text/plain";
        if (this._list != null)
        {
            // You still have to define serialization
            var text = string.Join(",", this._list.Select(x => string.Format("[{0},{1}]", x.hr, x.ps)));
            response.Write(text);
        }
    }
}
公共类代码结果:ActionResult
{
私有只读列表_List;
公共代码结果(列表)
{
这个._list=list;
}
公共覆盖无效ExecuteSult(ControllerContext上下文)
{
if(上下文==null)
抛出新的ArgumentNullException(“上下文”);
var response=context.HttpContext.response;
response.ContentType=“text/plain”;
如果(此._列表!=null)
{
//您仍然必须定义序列化
var text=string.Join(“,”,this._list.Select(x=>string.Format(“[{0},{1}]”,x.hr,x.ps));
回答:写(文本);
}
}
}
然后使用它:

public ActionResult Get()
{
    string data = myclient.DownloadString("URL");
    List<Codes> myobj = (List<Codes>)JsonConvert.DeserializeObject(data, typeof(List<Codes>));
    return new CodesResult(myobj);
}
public ActionResult Get()
{
string data=myclient.DownloadString(“URL”);
List myobj=(List)JsonConvert.DeserializeObject(数据,typeof(List));
返回新代码结果(myobj);
}
我不知道您的任务是什么,以及为什么需要返回自定义纯文本。不过,我建议您使用。如果启用了intendation,它将生成非常可读的json。而且,对于任何复杂的类型,都可以很容易地重用它


另外,如果您需要返回数据,没有任何GUI等,请查看。

您需要向我们展示视图中的代码,我认为这是因为您可能会混淆了ApiController和Controller,但我不确定。你能解释一下你希望实现的目标吗?@Massanu谢谢你的回答,我没有补充任何观点。我正在尝试从控制器返回此列表。是否可以从控制器返回。@Danny我正在尝试显示控制器中显示的简单列表。您可以使用WebAPI,但我不确定是否可以使用MVC控制器返回。@Iorond我正在尝试将Json字符串反序列化到列表对象并在浏览器上显示它。我有这样的JSON字符串:[{“hr”:“AA”,“PS”:“BB”},{“hr”:“AA”,“PS”:“BB”},{“hr”:“AA”,“PS”:“BB”},{“hr”:“AA”,“PS”:“BB”}],我使用Jsonconvert.deserialize方法将上述字符串反序列化到列表中,并希望列表显示为:[AA,BB],[AA,BB],[AA,BB]。@SwethaD您将其反序列化为什么类型?您希望它如何返回到浏览器?作为html、json、纯文本?还是别的什么?您最好显示完整的代码。@Iorond我反序列化为列表对象后,我想以纯文本的形式返回[AA,BB],[AA,BB],[AA,BB]。@loronod我在问题中添加的代码太大,无法添加注释,非常感谢您的回复…@loronod非常感谢您的跟进,但是当我尝试以下选项时,我仍然看到列表的类型,而不是列表的内容:public ActionResult GetListText(){var list=new list{“AA”、“BB”、“CC”};返回this.Content(string.Join(“,”,list));}
public ActionResult Get()
{
    string data = myclient.DownloadString("URL");
    List<Codes> myobj = (List<Codes>)JsonConvert.DeserializeObject(data, typeof(List<Codes>));

    // Let's convert it into what you want.
    var text = string.Join(",", list.Select(x => string.Format("[{0},{1}]", x.hr, x.ps)));
    return this.Content(text);
}
public class CodesResult : ActionResult
{
    private readonly List<Codes> _list;

    public CodesResult(List<Codes> list)
    {
        this._list = list;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");

        var response = context.HttpContext.Response;

        response.ContentType = "text/plain";
        if (this._list != null)
        {
            // You still have to define serialization
            var text = string.Join(",", this._list.Select(x => string.Format("[{0},{1}]", x.hr, x.ps)));
            response.Write(text);
        }
    }
}
public ActionResult Get()
{
    string data = myclient.DownloadString("URL");
    List<Codes> myobj = (List<Codes>)JsonConvert.DeserializeObject(data, typeof(List<Codes>));
    return new CodesResult(myobj);
}