在mvc4应用程序中使用类数组作为操作的输入,无法完全读取查询字符串数据

时间:2021-05-28 19:45:18

I have made a mvc4 application and I have a Controller that outputs a png file like this:

我已经制作了一个mvc4应用程序,我有一个控制器输出一个像这样的png文件:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Web.Mvc;
using Foobar.Classes;

namespace Foobar.Controllers
{
  public class ImageController : Controller
  {
    public ActionResult Index(Label[] labels)
    {
      var bmp = new Bitmap(400, 300);
      var pen = new Pen(Color.Black);
      var font = new Font("arial", 20);

      var g = Graphics.FromImage(bmp);

      g.SmoothingMode = SmoothingMode.HighQuality;
      g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

      if (labels != null)
      {
        g.DrawString("" + labels.Length, font, pen.Brush, 20, 20);

        if (labels.Length > 0)
        {
          g.DrawString("" + labels[0].label, font, pen.Brush, 20, 40);
        }
      }

      var stream = new MemoryStream();
      bmp.Save(stream, ImageFormat.Png);

      stream.Position = 0;

      return File(stream, "image/png");
    }
  }
}

The Label class looks like this:

Label类看起来像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Foobar.Classes
{
  public class Label
  {
    public string label { get; set; }
    public int fontsize { get; set; }
  }
}

When I run my controller having this in the url:

当我在url中运行我的控制器时:

http://localhost:57775/image?labels[0][label]=Text+rad+1&labels[0][fontsize]=5&labels[1][fontsize]=5&labels[2][fontsize]=5

I get the correct amount of labels, so the image will show 3. But the instances of Label will not get its data members fill in. I have also tried to do this using plain variables (not properties).

我得到了正确数量的标签,因此图像将显示3.但Label的实例不会让其数据成员填写。我也尝试使用普通变量(不是属性)来做到这一点。

If they were filled in, the image would actually show "3" and "Text rad 1".

如果它们被填写,图像实际上将显示“3”和“文本rad 1”。

So what do I put in the class "Label" to have the properties right? Should there be some kind of annotation?

那么我应该在“Label”类中添加哪些属性呢?应该有某种注释吗?

Where do I read about this?

我在哪里读到这个?

1 个解决方案

#1


0  

I got it, the querystring was wrong, should be this:

我明白了,查询字符串错了,应该这样:

http://localhost:57775/image?labels[0].label=Text+rad+1&labels[0].fontsize=5&labels[1].fontsize=5&labels[2].fontsize=5

#1


0  

I got it, the querystring was wrong, should be this:

我明白了,查询字符串错了,应该这样:

http://localhost:57775/image?labels[0].label=Text+rad+1&labels[0].fontsize=5&labels[1].fontsize=5&labels[2].fontsize=5