【问题标题】:ASP.NET MVC Model binding from Querystring to Object and Request-response pattern从查询字符串到对象和请求响应模式的 ASP.NET MVC 模型绑定
【发布时间】:2012-06-27 10:06:51
【问题描述】:

HTTP POST 参数中的模型绑定与请求-响应模式配合得很好,因此我在控制器中接收到我需要的 ViewModel,并使用该对象调用服务层。 所以一切都封装在 DTO ViewModel 中。如果我想添加一些其他参数,我会修改对象而不是方法声明。

如果可能,我需要使用 HTTP GET 请求(来自 QueryString)自动执行相同的操作 例如:

/Index/CountryName/PageNumber/1 绑定到控制器 Index(string CountryName, int PageNumber)

我希望它绑定到这个控制器到:Index(CountryQueryStringModel countryQueryStringModel)

class CountryQueryStringModel 
{
   public string CountryName, 
   public int PageNumber 
}

使用此方法,如果我想添加例如过滤条件,我将其封装在对象 CountryQueryStringModel

感谢您的帮助。

【问题讨论】:

  • 您有什么问题吗?如果您已正确设置路由,默认模型绑定器仍将查询字符串值绑定到模型吗?
  • 嗯,你的意思是我可以在路由过程中做到这一点?路由如何将对象返回给控制器。

标签: asp.net asp.net-mvc model-binding


【解决方案1】:

你是对的马克,路由默认模型绑定器做到了。 这是我找到的解决方案

context.MapRoute(
                null,
                "hotel/{countryName}/Page/{pageNumber}",
                new { controller = "ResultsCity", action = "Index"},
                new[] { "California_Front.Areas.Hotel_FR.Controllers" }

context.MapRoute(
                null,
                "hotel/{countryName}/",
                new { controller = "ResultsCity", action = "Index", PageNumber = 1 },
                new[] { "California_Front.Areas.Hotel_FR.Controllers" }

控制器是这样的

public ActionResult Index(CountryQueryStringModel CountryQueryStringModel)
{}

感谢您的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    相关资源
    最近更新 更多