我想我在ASP.NET MVC控制器的参数填充中发现了一个错误
public JsonResult Lookup(
string q_word, string primary_key,
int per_page, int page_num)
如果q_word发布的值为空字符串,则q_word将收到空字符串。如果将这些参数打包在一起(DRY原则),则行为不同,空字符串变为空。
public class LookupArg
{
public string q_word { get; set; }
public string primary_key { get; set; }
public int per_page { get; set; }
public int page_num { get; set; }
public string another_word { get; set; }
}
public JsonResult TesterA(
string q_word, string another_word, string primary_key,
int per_page, int page_num)
{
return Json(
new { q_word, primary_key, per_page, page_num, another_word},
JsonRequestBehavior.AllowGet);
}
public JsonResult TesterB(LookupArg la)
{
return Json(
new { la.q_word, la.primary_key, la.per_page, la.page_num,
la.another_word },
JsonRequestBehavior.AllowGet);
}
http://localhost:19829/Product/TesterA?q_word=&primary_key=id&per_page=10&page_num=1&another_word= 有这个输出:
{"q_word":"","primary_key":"id","per_page":10,"page_num":1,"another_word":""}
http://localhost:19829/Product/TesterB?q_word=&primary_key=id&per_page=10&page_num=1&another_word= 有这个输出:
{"q_word":null,"primary_key":"id","per_page":10,"page_num":1,"another_word":null}
我也试过这个,但无济于事,同样的输出,q_word和another_word仍然是空的
public JsonResult TesterB(
[Bind(Include = "q_word, primary_key, per_page, page_num, another_word")]
LookupArg la)
是否期望这种行为?按设计?如果某个值来自对象,是否会有任何差异?
答案 0 :(得分:0)
如果要覆盖自ASP.NET MVC 2以来设计的这种行为,您可以使用[DisplayFormat]
属性修饰该属性(在ASP.NET MVC 1中不是这种情况,您可以结帐following blog post):
public class LookupArg
{
[DisplayFormat(ConvertEmptyStringToNull = false)]
public string q_word { get; set; }
...
}