Cant full read querystring data in mvc4 application using a class array as input to action
- by Anders Lindén
I have made a mvc4 application and I have a Controller that outputs a png file like this:
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:
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:
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).
If they were filled in, the image would actually show "3" and "Text rad 1".
So what do I put in the class "Label" to have the properties right? Should there be some kind of annotation?
Where do I read about this?