简体   繁体   English

清单 <T> 传递给控制器​​发送空项目

[英]List<T> passed to controller sends empty items

A PartialView contains a model Foo with a List<Bar> . PartialView包含一个具有List<Bar>的模型Foo Each Bar item contains two properties, BarA ( string ) and BarB (decimal). 每个Bar项都包含两个属性,即BarAstring )和BarB (十进制)。
I am trying to render a Chart in that partial view, but for that to work I need to call an action on the same Controller and pass the result to an <img /> element. 我试图在该局部视图中呈现一个Chart,但是要使其正常工作,我需要在同一Controller上调用一个动作,并将结果传递给<img />元素。 To render the chart, I need the collections of BarA and BarB to format the data. 要渲染图表,我需要BarABarB的集合来格式化数据。
So I'm trying with something like this: 所以我正在尝试这样的事情:

Controller 调节器

public void GenerateChart(List<Bar> model)
{
    var chart = new System.Web.Helpers.Chart(400, 200)
    .AddTitle("Title")
    .AddSeries(
        name : "name",
        xValue : model.Select(m => m.BarA).ToArray(),
        yValues : model.Select(m => m.BarB).ToArray())
    .Write();
}

Partial View 部分视图

RouteValueDictionary rvd = new RouteValueDictionary();
for (int i = 0; i < Model.Bars.Count; ++i)
{ rvd.Add("model[" + i + "]", Model.Bars[i]); }

<img src="@Url.Action("GenerateChart", rvd)" />

The problem with this is that even though the model object contains the three items it should contain, these are null. 这样做的问题是,即使模型对象包含它应包含的三个项目,这些也是空的。
I also tried to use the ViewBag, like this: 我也尝试使用ViewBag,如​​下所示:

ViewBag.BarA = Model.Bars.Select(m => m.BarA).ToArray();
ViewBag.BarB = Model.Bars.Select(m => m.BarB).ToArray();

With this on the controller side 在控制器方面

public void GenerateChart()
{
    var chart = new System.Web.Helpers.Chart(400, 200)
    .AddTitle("Title")
    .AddSeries(
        name : "name",
        xValue : ViewBag.BarA,
        yValues : ViewBag.BarB)
    .Write();
}

But both arrays are null. 但是两个数组都为空。 I've also tried a few different ideas but I'm not able to get the information I need. 我也尝试了一些不同的想法,但无法获得所需的信息。

To triple-check (the data is shown fine in the view) that the data is correct, I changed to this: 为了三重检查(数据在视图中显示正常)数据是否正确,我将其更改为:

@{
    string[] barAs = Model.Select(m => m.BarA).ToArray();
    decimal[] barBs = Model.Select(m => m.BarB).ToArray();
    ViewBag.BarAs = barAs; // this has 3 items with the expected data
    ViewBag.BarBs = barBs; // this also works
}
<img src="@Url.Action("GenerateChart")" />

string[] BarAs = ViewBag.BarAs; // this assigns null
decimal[] BarBs = ViewBag.BarBs; // this also assigns null

It seems you don't really understand how MVC works. 看来您不太了解MVC的工作原理。 I encourage you to spend some time going through all the tutorials at http://asp.net/mvc to familiarize yourself with the framework. 我鼓励您花一些时间浏览http://asp.net/mvc上的所有教程,以熟悉该框架。 Namely, it seems you're trying to approach a lot of this as if you were in the world of Web Forms. 即,似乎您正在尝试采用很多方法,就像您在Web窗体领域一样。 MVC is an entirely different beast. MVC是完全不同的野兽。

First, Html.Action cannot return a full image, because all it's going to do is just dump the return value to the HTML being generated, and you can't embed a binary object directly in HTML. 首先, Html.Action无法返回完整图像,因为它要做的只是将返回值转储到正在生成的HTML中,并且您不能直接在HTML中嵌入二进制对象。

Second, even if you could , you can't use that as the src for an img tag. 其次,即使可以 ,也不能将其用作img标签的src The src must be a string, namely a URL, point to a location of an image. src必须是一个字符串,即URL,指向图像的位置。

So, in order to achieve this. 因此,为了实现这一目标。 You will need a full action that returns a proper response as an image. 您将需要执行完整的操作,以将适当的响应作为图像返回。 Then, you can simply link your image src to the route that hits this action. 然后,您可以简单地将图像src链接到执行此操作的路由。

public ActionResult GenerateChart(List<Bar> model)
{
    var chart = new System.Web.Helpers.Chart(400, 200)
        .AddTitle("Title")
        .AddSeries(
            name : "name",
            xValue : model.Select(m => m.BarA).ToArray(),
            yValues : model.Select(m => m.BarB).ToArray())
    .GetBytes("jpeg");

    return File(chart, "image/jpeg");
}

Then, 然后,

<img src="@Url.Action("GenerateChart", new { model = rvd })" alt="" />

Now, you're just link to a URL. 现在,您只需链接到URL。 That URL maps to a route that hits your GenerateChart action, which then returns an actual image - same as if you directly linked to a physical image. 该URL映射到命中您的GenerateChart操作的路由,该路由然后返回实际图像-就像您直接链接到物理图像一样。 Now, the browser can properly render the img tag to the page. 现在,浏览器可以正确地将img标签呈现到页面上。

Passing a complex type to actions via GET request is technically bloody thing so I do not know if this solution fits your needs you can follow up the method below; 通过GET请求将复杂类型传递给操作在技术上是一件很血腥的事情,因此我不知道此解决方案是否适合您的需求,您可以按照以下方法进行操作;

Your action will recieve model as serialized string and you have to Deserialize it to your model

public ActionResult GenerateChart(string modelAsString)
{
    List<Bar> model = new List<Bar>();
    model = JsonConvert.DeserializeObject<List<Bar>>(modelAsString);

    var chart = new System.Web.Helpers.Chart(400, 200)
    .AddTitle("Title")
    .AddSeries(
        name: "name",
        xValue: model.Select(m => m.BarA).ToArray(),
        yValues: model.Select(m => m.BarB).ToArray())
    .GetBytes("jpeg");

    return File(chart, "image/jpeg");
}

Then you need to call your action via querystring like ?modelAsString={jsonData} The example URL I use to process data : http://localhost:18681/Home/GenerateChart/?modelAsString=[{%22BarA%22:%22barAData%22,%22BarB%22:1.1},{%22BarA%22:%22barAData2%22,%22BarB%22:441.14},{%22BarA%22:%22barAData43%22,%22BarB%22:44.1}] Then you need to call your action via querystring like ?modelAsString={jsonData}我用于处理数据的示例URL: http://localhost:18681/Home/GenerateChart/?modelAsString=[{%22BarA%22:%22barAData%22,%22BarB%22:1.1},{%22BarA%22:%22barAData2%22,%22BarB%22:441.14},{%22BarA%22:%22barAData43%22,%22BarB%22:44.1}]

You should create your <img> URLs via serializing your model which ready on the page's action which you use <img> s. 您应该通过序列化模型来创建<img> URL,该模型已准备好在使用<img>的页面操作中显示。

I have tested a dummy data you can see output below; 我已经测试了一个虚拟数据,您可以在下面看到输出; 虚拟示例

PS: for creating dummy data I used the method below; PS:为了创建虚拟数据,我使用了以下方法;

public ActionResult DummyData()
{
    List<Bar> model = new List<Bar>();
    model.Add(new Bar() { BarA = "barAData", BarB = 1.1m });
    model.Add(new Bar() { BarA = "barAData2", BarB = 441.14m });
    model.Add(new Bar() { BarA = "barAData43", BarB = 44.1m });
    return Json(model, JsonRequestBehavior.AllowGet);
}

And I wonder too if any more efficient way to do this via get request. 我也想知道是否有更有效的方法通过获取请求来做到这一点。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM