I'm passing json back up from my view to my controller actions to perform operations. To convert the json being sent in, to a POCO I'm using this Action Filter:
我将json从视图传回到控制器操作。要将发送进来的json转换为POCO,我使用这个操作过滤器:
public class ObjectFilter : ActionFilterAttribute {
public Type RootType { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext) {
IList<ErrorInfo> errors = new List<ErrorInfo>();
try {
object o = new DataContractJsonSerializer(RootType).ReadObject(filterContext.HttpContext.Request.InputStream);
filterContext.ActionParameters["postdata"] = o;
}
catch (SerializationException ex) {
errors.Add(new ErrorInfo(null, ex.Message));
}
finally {
filterContext.ActionParameters["errors"] = errors.AsEnumerable();
}
}
}
It's using the DataContractJsonSerializer to map the JSON, over to my object. My Action is then decorated like so:
它使用DataContractJsonSerializer将JSON映射到我的对象。我的行为被装饰成这样:
[ObjectFilter(RootType = typeof(MyObject))]
public JsonResult updateproduct(MyObject postdata, IEnumerable<ErrorInfo> errors) {
// check if errors has any in the collection!
}
So to surmise, what is going on here, if there is a problem serializing the JSON to the type of object (if a string cannot be parsed as a decimal type or similar for eg), it adds the error to a collection and then passes that error up to the view. It can then check if this collection has an errors and report back to the client.
推测,这是怎么回事,如果有问题的JSON序列化对象的类型(如果无法解析字符串作为如十进制类型或类似),它添加了错误集合,然后将错误的观点。然后,它可以检查该集合是否有错误并向客户端报告。
The issue is that I cannot seem to find out which field has caused the problem. Ideally I'd like to pass back to the view and say "THIS FIELD" had a problem. The SerializationException class does not seem to offer this sort of flexibility.
问题是我似乎不知道是哪个领域造成了这个问题。理想情况下,我希望返回到视图,并说“这个字段”有问题。SerializationException类似乎没有提供这种灵活性。
How would the collective SO hivemind consider tackling this problem?
这样的集体怎么会考虑解决这个问题呢?
2 个解决方案
#1
1
I would just do an ajax form post. It's much easier.
我只写一个ajax表单帖子。这是容易得多。
http://plugins.jquery.com/project/form
http://plugins.jquery.com/project/form
http://malsup.com/jquery/form/
http://malsup.com/jquery/form/
#2
1
How about this: Json.Net It reads a JSON string and then Deserialises it to the given POCO object.
这个怎么样:Json。Net读取JSON字符串,然后将其反序列化到给定的POCO对象。
string jsonResult = GetJsonStringFromSomeService();
MyPocoObject myobject = JsonConvert.DeserializeObject<MyPocoObject>(jsonResult);
Console.Write("Damn that is easy");
But for the determing where errors occur, I am not too sure.
但是对于发生错误的地方,我不太确定。
#1
1
I would just do an ajax form post. It's much easier.
我只写一个ajax表单帖子。这是容易得多。
http://plugins.jquery.com/project/form
http://plugins.jquery.com/project/form
http://malsup.com/jquery/form/
http://malsup.com/jquery/form/
#2
1
How about this: Json.Net It reads a JSON string and then Deserialises it to the given POCO object.
这个怎么样:Json。Net读取JSON字符串,然后将其反序列化到给定的POCO对象。
string jsonResult = GetJsonStringFromSomeService();
MyPocoObject myobject = JsonConvert.DeserializeObject<MyPocoObject>(jsonResult);
Console.Write("Damn that is easy");
But for the determing where errors occur, I am not too sure.
但是对于发生错误的地方,我不太确定。