不要绑定一个动作参数
我有一个如下所示的操作:
[Post]
[PopulateModelFromId]
public ActionResult ChangeName( string name, MyModel model )
{
try
{
model.changeName
return JSONSuccess();
}
catch( ModelUpdateException )
{
return JSONFail();
}
}
名称和模型 id 通过 ajax POST 发送,模型由自定义操作过滤器填充,该过滤器获取 id 并从数据库检索模型。
ActionFilter 看起来像这样:
...
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
// parse the id from the request
MyModel model = getModelFromDataStoreById( id );
filterContext.ActionParameters["model"] = model;
}
...
问题是 MyModel 对象没有无参数构造函数,MVC 甚至在调用 ActionFilter 之前尝试创建并绑定到 MyModel 对象,但会抛出异常,因为它无法实例化 MyModel 对象。
我的第一个问题是我这样做是否正确,或者我应该使用 HttpContext.Items 之类的东西在过滤器和操作之间传输数据?其次,有没有办法告诉 MVC 不要尝试绑定 MyModel 对象,因为它稍后会创建?
I have an action that looks like this:
[Post]
[PopulateModelFromId]
public ActionResult ChangeName( string name, MyModel model )
{
try
{
model.changeName
return JSONSuccess();
}
catch( ModelUpdateException )
{
return JSONFail();
}
}
The name and model id are sent by an ajax POST, and the model is populated by a custom action filter that takes the id and retrieves the model from the database.
The actionfilter looks like this:
...
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
// parse the id from the request
MyModel model = getModelFromDataStoreById( id );
filterContext.ActionParameters["model"] = model;
}
...
The problem is that the MyModel object doesn't have a parameterless constructor, and MVC is attempting to create and bind to the MyModel object before the ActionFilter is even called but throws an exception because it cannot instantiate the MyModel object.
My first question is am I doing this properly or should I be using something like HttpContext.Items to transfer data between filter and action? Second, is there a way to tell MVC to not try to bind the MyModel object because it will be created later?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自定义模型绑定器似乎比自定义操作过滤器更适合此任务:
您将在
Application_Start
中注册:现在您的控制器操作可能如下所示:
A custom model binder seems more appropriate for this task than a custom action filter:
which yuo would register in
Application_Start
:Now your controller action might look like this: