我可以在 ASP.NET MVC UpdateModel 发生之前对 POST 数据执行一些处理吗?

发布于 2024-09-02 06:43:42 字数 1114 浏览 3 评论 0原文

我想在使用 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 技术交流群。

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

发布评论

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

评论(2

风尘浪孓 2024-09-09 06:43:42

您可以接受发布的 FormCollection 并使用它,而不是在操作方法的参数中使用自动模型绑定。您可以 (1) 修改此特殊集合中的值,然后 (2) 使用 UpdateModel/TryUpdateModel 手动绑定模型。

例如,

public ActionResult Index(FormCollection formCollection)
{
    DoWhateverToFormCollection(formCollection);
    Team model;
    // TO-DO: Use TryUpdateModel here and handle more nicely
    // Should also pass in binding whitelist/blacklist to the following, if didn't remove from the formCollection already...
    UpdateModel<Team>(model, formCollection);    
    // rest of your code...

}

希望这应该像广告中所说的那样工作,祝你好运!

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,

public ActionResult Index(FormCollection formCollection)
{
    DoWhateverToFormCollection(formCollection);
    Team model;
    // TO-DO: Use TryUpdateModel here and handle more nicely
    // Should also pass in binding whitelist/blacklist to the following, if didn't remove from the formCollection already...
    UpdateModel<Team>(model, formCollection);    
    // rest of your code...

}

Hopefully this should work as advertised, and best of luck!

始于初秋 2024-09-09 06:43:42

我相信您可以为此使用自定义模型绑定器。 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.

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