MVC3 将查询字符串参数绑定到深层对象属性

发布于 2024-10-29 01:06:09 字数 801 浏览 3 评论 0原文

我的域设置类似于

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必了 2024-11-05 01:06:09

您的方法签名不应该是...

public ActionResult Index(int? page)
{
    var model = new IndexViewModel{
                        Pagination = new Pagination { Page = page ?? 1 } };
    if(page.HasValue)
        model.Stuff = StuffGenerator
                          .GetStuff()
                          .Skip(page.Value * _pageSize)
                          .Take(_pageSize);
    else
        model.Stuff = StuffGenerator.GetStuff().take(_pageSize);
    return View(model);
}

您的示例听起来像 GET 但看起来像 POST。

Shouldn't your method signature be...

public ActionResult Index(int? page)
{
    var model = new IndexViewModel{
                        Pagination = new Pagination { Page = page ?? 1 } };
    if(page.HasValue)
        model.Stuff = StuffGenerator
                          .GetStuff()
                          .Skip(page.Value * _pageSize)
                          .Take(_pageSize);
    else
        model.Stuff = StuffGenerator.GetStuff().take(_pageSize);
    return View(model);
}

Your example sounds like a GET but looks like a POST.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文