如何在动作期间绑定模型?

How to bind model during an action?

我有一个动态 url 处理器

public ActionResult DynamicUrl(string slug = null)

此方法适用于 slug(顺便说一句,slug 代表什么?)并确定 slug 是在展示产品还是在执行产品搜索。

作为产品搜索的一部分,我有一个 page=1 查询字符串参数。

E.g. /Womens/Dresses?page=2

通常我会在将页面查询字符串绑定到 ProductSearch 模型的正常产品搜索操作中执行此操作。

public ActionResult Results(ProductSearchModel  searchModel)

如何在动作中绑定querstring?例如

public ActionResult DynamicUrl(string slug = null)
{
    ProductSearchModel psm = new ProductSearchModel();

    //Auto bind psm here.
    // E.g. Controller.BindModel(psm);
}

希望我没有偏离正轨。

你的意思是:

UpdateModel(psm);

这会将当前表单集合绑定到指定的模型。

您还可以使用:

TryUpdateModel(psm);

这个版本不会在出现问题时抛出异常 returns truefalse.