MVC3 将查询字符串参数绑定到深层对象属性
我的域设置类似于
public class Pagination
{
public int? Page { get; set; }
}
public class IndexViewModel
{
public Pagination Pagination { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index(IndexViewModel model, Pagination pg, string page)
{
return View(model);
}
}
当我导航到 /?Page=5
时,我希望 model.Pagination.Page 的值也为 5,但 MVC 似乎不绑定查询参数深度超过1层。
我可以做什么来改变这种情况?
或者改变这个设置是否会带来更多麻烦而不值得?我应该这样做
public class HomeController : Controller
{
public ActionResult Index(IndexViewModel model, Pagination pg, string page)
{
model.Pagination = pg;
return View(model);
}
}
*注意三重参数是为了说明它不会填充 IndexViewModel 但它会填充其他两个参数,因为它们是 0 或 1 层深。
My domain is setup similar to
public class Pagination
{
public int? Page { get; set; }
}
public class IndexViewModel
{
public Pagination Pagination { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index(IndexViewModel model, Pagination pg, string page)
{
return View(model);
}
}
When I navigate to /?Page=5
I would expect 5 to be the value of model.Pagination.Page to be 5 also, however it appears MVC does not bind query parameters more than 1 layer deep.
What can do I do to change this?
Or is changing this setting up more trouble than it's worth? And I should just do
public class HomeController : Controller
{
public ActionResult Index(IndexViewModel model, Pagination pg, string page)
{
model.Pagination = pg;
return View(model);
}
}
*Note the triple parameters are there to illustrate that it won't fill IndexViewModel but it fills both of the other parameters since they're 0 or 1 layer deep.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的方法签名不应该是...
您的示例听起来像 GET 但看起来像 POST。
Shouldn't your method signature be...
Your example sounds like a GET but looks like a POST.