将数据绑定到 Asp.net MVC 中的现有对象

发布于 2024-08-22 13:57:26 字数 659 浏览 13 评论 0原文

在 ASP.Net MVC 模型绑定器中,可以创建绑定类型的对象,然后更新其属性。

例如,

public override object BindModel(ControllerContext controllerContext,
    ModelBindingContext bindingContext)
{
    ParentType boundModel = null;
    if (bindingContext.ModelType == typeof(ParentType))
    {
        var myFactory = new MyFactory();
        var someValue = bindingContext.ValueProvider.GetValue
            ("someFieldId").AttemptedValue;
        ChildType child = myFactory.Create(someValue);
        BindModel(child);
        boundModel = child;
    }
    return boundModel;
}

在这段代码中,我想知道是否有类似于 BindModel(child) 调用的东西,有点像来自控制器的 TryModelUpdate() ?

Within an ASP.Net MVC model binder is it possible to create an object of the bound type and then update the properties on it.

e.g.

public override object BindModel(ControllerContext controllerContext,
    ModelBindingContext bindingContext)
{
    ParentType boundModel = null;
    if (bindingContext.ModelType == typeof(ParentType))
    {
        var myFactory = new MyFactory();
        var someValue = bindingContext.ValueProvider.GetValue
            ("someFieldId").AttemptedValue;
        ChildType child = myFactory.Create(someValue);
        BindModel(child);
        boundModel = child;
    }
    return boundModel;
}

In this code I want to know if there is something similar to the BindModel(child) call, kind of like TryModelUpdate() from a controller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

书信已泛黄 2024-08-29 13:57:26

我认为解决您的问题的更好方法是从 DefaultModelBinder 派生(我认为您可能是),然后重写 CreateModel 方法而不是 BindModel 方法。

通过从中返回您的 Child 对象,您应该沿着您正在寻找的路径前进。

I think a better approach to your problem would be to derive from the DefaultModelBinder (which I think you probably are) and then override the CreateModel method rather than the BindModel method.

By returning your Child object from that, you should be along the path you're looking for.

北风几吹夏 2024-08-29 13:57:26
public override object BindModel(ControllerContext controllerContext, 
                                 ModelBindingContext bindingContext)
{
    ParentType boundModel = null;
    if (bindingContext.ModelType == typeof(ParentType))
    {
        var myFactory = new MyFactory();
        var someValue = bindingContext.ValueProvider
                                      .GetValue("someFieldId").AttemptedValue;
        ChildType child = myFactory.Create(someValue);
        BindModel(child);
        boundModel = child;
    }

    // change here
    bindingContext.ModelMetadata.Model = boundModel;
    return BindModel(controllerContext, bindingContext);
}

这个怎么样?

public override object BindModel(ControllerContext controllerContext, 
                                 ModelBindingContext bindingContext)
{
    ParentType boundModel = null;
    if (bindingContext.ModelType == typeof(ParentType))
    {
        var myFactory = new MyFactory();
        var someValue = bindingContext.ValueProvider
                                      .GetValue("someFieldId").AttemptedValue;
        ChildType child = myFactory.Create(someValue);
        BindModel(child);
        boundModel = child;
    }

    // change here
    bindingContext.ModelMetadata.Model = boundModel;
    return BindModel(controllerContext, bindingContext);
}

How about this?

無心 2024-08-29 13:57:26

绑定器只需要类属性和字段值中的名称相同,调用 updatemodel 会将任何匹配的值放入模型中。

所以你应该能够创建任一类型的类并调用 updatemodel ???

The binder simply need the same names in class properties and the field value, calling updatemodel will place any matching values into the model.

So you should be able to just create either type of class and call updatemodel ???

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