我將JSON發佈到控制器方法,並且它看起來好像JsonValueProvider包含在ASP.Net MVC 3中時,它試圖將JSON數字轉換爲字節類型。ASP.net MVC JsonValueProvider字節類型問題
基本上,發生的情況是任何需要轉換爲字節的非空值都爲空,並且我的模型狀態有錯誤且沒有錯誤消息。正如返回JSON中的問題列表所示。 dnbscore和地區有錯誤,但評級不會。返回的數據有dnbscore和region作爲null,即使JSON通過時,這些字段也有值。我可以用一個自定義模型綁定器來處理這個問題,但是我想知道是否有另一種方法來使JsonValueProider像我期望的那樣工作。
這是我試圖建立對象:
public class APICustomerDetailsDTO
{
public int customerid { get; set; }
[StringLength(30)]
public string customername { get; set; }
public decimal? globalcredit { get; set; }
[Range(0,5)]
public byte? rating { get; set; }
[Range(0, 100)]
public byte? dnbscore { get; set; }
[Range(0, 8)]
public byte? region { get; set; }
[Required]
public bool isnotactive { get; set; }
public string salesperson { get; set; }
[StringLength(30)]
public string newmill_venderid { get; set; }
[StringLength(30)]
public string newmill_supplierid { get; set; }
[StringLength(30)]
public string edi_receiverid { get; set; }
[Required]
public bool edi_invoice { get; set; }
[StringLength(15)]
public string bill_code { get; set; }
}
這是我在POST請求我發送JSON:用於檢查模型狀態
{"bill_code":"good","customerid":50,"customername":"Ted","dnbscore":80,"edi_invoice":false,"edi_receiverid":null,"globalcredit":null,"isnotactive":false,"newmill_supplierid":null,"newmill_venderid":null,"rating":null,"region":0,"salesperson":null}
我的方法的一部分:
if (!ModelState.IsValid)
{
var issues = ModelState.Where(m => m.Value.Errors.Any())
.Select((m)=> new {field = m.Key, error = m.Value.Errors.FirstOrDefault().ErrorMessage})
.ToArray();
var result = new
{
result = "Failure",
message = "Invalid data received. See issues for details.",
issues = issues,
data = cust
};
返回JSON:
{"result":"Failure","message":"Invalid data received. See issues for details.","issues":[{"field":"dnbscore","error":""},{"field":"region","error":""}],"data":{"customerid":50,"customername":"Ted","globalcredit":null,"rating":null,"dnbscore":null,"region":null,"isnotactive":false,"salesperson":null,"newmill_venderid":null,"newmill_supplierid":null,"edi_receiverid":null,"edi_invoice":false,"bill_code":"good"}}
爲了完整這裏的緣故是我做過什麼來解決這個問題:
由達林提供的信息,我意識到這是一個更大的問題不僅僅是字節?爲int?轉換。這對於由默認聯編程序提供的任何非整型轉換都是一個問題。正因爲如此,我做了一個自定義活頁夾,它應該處理字節,小數等數字。請看下圖:
public class APICustomerDetailsDTOBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
APICustomerDetailsDTO model = (APICustomerDetailsDTO)bindingContext.Model ??
(APICustomerDetailsDTO)DependencyResolver.Current.GetService(typeof(APICustomerDetailsDTO));
bool hasPrefix = bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName);
string searchPrefix = (hasPrefix) ? bindingContext.ModelName + "." : "";
int customerid = 0;
int.TryParse(GetValue(bindingContext, searchPrefix, "customerid"), out customerid);
model.customerid = customerid;
string customername = GetValue(bindingContext, searchPrefix, "customername");
if (!String.IsNullOrEmpty(customername))
{ model.customername = customername; }
else
{ model.customername = null; }
decimal globalcredit;
if (decimal.TryParse(GetValue(bindingContext, searchPrefix, "globalcredit"), out globalcredit))
{ model.globalcredit = globalcredit; }
else
{ model.globalcredit = null; }
byte rating;
if (byte.TryParse(GetValue(bindingContext, searchPrefix, "rating"), out rating))
{ model.rating = rating; }
else
{ model.rating = null; }
byte dnbscore;
if (byte.TryParse(GetValue(bindingContext, searchPrefix, "dnbscore"), out dnbscore))
{ model.dnbscore = dnbscore; }
else
{ model.dnbscore = null; }
byte region;
if (byte.TryParse(GetValue(bindingContext, searchPrefix, "region"), out region))
{ model.region = region; }
else
{ model.region = null; }
bool isnotactive;
if (bool.TryParse(GetValue(bindingContext, searchPrefix, "isnotactive"), out isnotactive))
{ model.isnotactive = isnotactive; }
else
{ model.isnotactive = false; }
string salesperson = GetValue(bindingContext, searchPrefix, "salesperson");
if (!String.IsNullOrEmpty(salesperson))
{ model.salesperson = salesperson; }
else
{ model.salesperson = null; }
string newmill_venderid = GetValue(bindingContext, searchPrefix, "newmill_venderid");
if (!String.IsNullOrEmpty(newmill_venderid))
{ model.newmill_venderid = newmill_venderid; }
else
{ model.newmill_venderid = null; }
string newmill_supplierid = GetValue(bindingContext, searchPrefix, "newmill_supplierid");
if (!String.IsNullOrEmpty(newmill_supplierid))
{ model.newmill_supplierid = newmill_supplierid; }
else
{ model.newmill_supplierid = null; }
string edi_receiverid = GetValue(bindingContext, searchPrefix, "edi_receiverid");
if (!String.IsNullOrEmpty(edi_receiverid))
{ model.edi_receiverid = edi_receiverid; }
else
{ model.edi_receiverid = null; }
bool edi_invoice;
if (bool.TryParse(GetValue(bindingContext, searchPrefix, "edi_invoice"), out edi_invoice))
{ model.edi_invoice = edi_invoice; }
else
{ model.edi_invoice = false; }
model.bill_code = GetValue(bindingContext, searchPrefix, "bill_code");
return model;
}
private string GetValue(ModelBindingContext context, string prefix, string key)
{
ValueProviderResult vpr = context.ValueProvider.GetValue(prefix + key);
return vpr == null ? null : vpr.AttemptedValue;
}
private bool GetCheckedValue(ModelBindingContext context, string prefix, string key)
{
bool result = false;
ValueProviderResult vpr = context.ValueProvider.GetValue(prefix + key);
if (vpr != null)
{
result = (bool)vpr.ConvertTo(typeof(bool));
}
return result;
}
}
這JSON序列化問題是我的問題到底是什麼。使用字符串的工作是有道理的,因爲Parse方法需要字符串值。我可以控制我發佈到頁面的數據,但是有什麼方法可以確保這些值是字符串,當我將它們轉換爲JSON開始時?你讓我意識到這個問題比字節更大?我認爲它被限制的領域。謝謝。 – LRFalk01 2012-07-16 20:04:06