我可以在 ASP.NET MVC UpdateModel 发生之前对 POST 数据执行一些处理吗?
我想在使用 UpdateModel
更新数据库中的副本之前从 POST 数据中删除非数字元素。有办法做到这一点吗?
// TODO: it appears I don't even use the parameter given at all, and all the magic
// happens via UpdateModel and the "controller's current value provider"?
[HttpPost]
public ActionResult Index([Bind(Include="X1, X2")] Team model) // TODO: stupid magic strings
{
if (this.ModelState.IsValid)
{
TeamContainer context = new TeamContainer();
Team thisTeam = context.Teams.Single(t => t.TeamId == this.CurrentTeamId);
// TODO HERE: apply StripWhitespace() to the data before using UpdateModel.
// The data is currently somewhere in the "current value provider"?
this.UpdateModel(thisTeam);
context.SaveChanges();
this.RedirectToAction(c => c.Index());
}
else
{
this.ModelState.AddModelError("", "Please enter two valid Xs.");
}
// If we got this far, something failed; redisplay the form.
return this.View(model);
}
抱歉,内容太简洁了,我整晚都在忙这个;希望我的问题足够清楚吗?也很抱歉,因为这是一个新手问题,我可能可以通过几个小时的文档拖网来解决,但我时间紧迫...... bleh。
I would like to strip out non-numeric elements from the POST data before using UpdateModel
to update the copy in the database. Is there a way to do this?
// TODO: it appears I don't even use the parameter given at all, and all the magic
// happens via UpdateModel and the "controller's current value provider"?
[HttpPost]
public ActionResult Index([Bind(Include="X1, X2")] Team model) // TODO: stupid magic strings
{
if (this.ModelState.IsValid)
{
TeamContainer context = new TeamContainer();
Team thisTeam = context.Teams.Single(t => t.TeamId == this.CurrentTeamId);
// TODO HERE: apply StripWhitespace() to the data before using UpdateModel.
// The data is currently somewhere in the "current value provider"?
this.UpdateModel(thisTeam);
context.SaveChanges();
this.RedirectToAction(c => c.Index());
}
else
{
this.ModelState.AddModelError("", "Please enter two valid Xs.");
}
// If we got this far, something failed; redisplay the form.
return this.View(model);
}
Sorry for the terseness, up all night working on this; hopefully my question is clear enough? Also sorry since this is kind of a newbie question that I might be able to get with a few hours of documentation-trawling, but I'm time-pressured... bleh.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以接受发布的 FormCollection 并使用它,而不是在操作方法的参数中使用自动模型绑定。您可以 (1) 修改此特殊集合中的值,然后 (2) 使用
UpdateModel
/TryUpdateModel
手动绑定模型。例如,
希望这应该像广告中所说的那样工作,祝你好运!
Instead of using the automatic model-binding in your action method's parameters, you could accept the posted FormCollection and work with that. You might be able to (1) modify the values in this special collection, then (2) bind your model manually, using
UpdateModel
/TryUpdateModel
.for example,
Hopefully this should work as advertised, and best of luck!
我相信您可以为此使用自定义模型绑定器。 Scott Hanselman 这里有一篇文章使用拆分 DateTime 的概念描述了该过程分成两个单独的部分作为示例。
I believe you can use a Custom Model Binder for this. Scott Hanselman has an article here that describes the process, using the concept of splitting a DateTime into two separate parts as an example.