ASP.NET MVC - [Bind(Exclude = "Id")] 的替代方案

发布于 2024-10-10 14:45:40 字数 217 浏览 5 评论 0 原文

是否有 [Bind(Exclude = "Id")] 的替代方案(相关问题)

我可以写一个模型活页夹吗?

Is there an alternative for [Bind(Exclude = "Id")] (Related Question) ?

Could I write a model binder?

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

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

发布评论

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

评论(5

北城挽邺 2024-10-17 14:45:40

是的,有:它称为视图模型。视图模型是专门针对给定视图的特定需求而定制的类。

因此,不要

public ActionResult Index([Bind(Exclude = "Id")] SomeDomainModel model)

使用:

public ActionResult Index(SomeViewModel viewModel)

,其中视图模型仅包含需要绑定的属性。然后您可以在视图模型和模型之间进行映射。可以使用 AutoMapper 简化此映射。

作为最佳实践,我建议您始终在视图中使用视图模型。

Yes there is: it's called view models. View models are classes which are specifically tailored to the specific needs of a given view.

So instead of:

public ActionResult Index([Bind(Exclude = "Id")] SomeDomainModel model)

use:

public ActionResult Index(SomeViewModel viewModel)

where the view model contains only the properties which need to be bound. Then you could map between the view model and the model. This mapping could be simplified with AutoMapper.

As best practice I would recommend you to always use view models to and from a view.

荒路情人 2024-10-17 14:45:40

您可以使用属性直接排除属性;

[BindNever]

You can exclude properties directly with an attribute using;

[BindNever]
ぇ气 2024-10-17 14:45:40

我想出了一个非常简单的解决方案。

public ActionResult Edit(Person person)
{
    ModelState.Remove("Id"); // This will remove the key 

    if (ModelState.IsValid)
       {
           //Save Changes;
       }
    }
}

A very simple solution that I figured out.

public ActionResult Edit(Person person)
{
    ModelState.Remove("Id"); // This will remove the key 

    if (ModelState.IsValid)
       {
           //Save Changes;
       }
    }
}
苏辞 2024-10-17 14:45:40

作为现有答案的补充,C# 6 可以以更安全的方式排除该属性:

public ActionResult Edit(Person person)
{
    ModelState.Remove(nameof(Person.Id));

    if (ModelState.IsValid)
       {
           //Save Changes;
       }
    }
}

public ActionResult Index([Bind(Exclude = nameof(SomeDomainModel.Id))] SomeDomainModel model)

As an addition to the existing answers, C# 6 makes it possible to exclude the property in a safer way:

public ActionResult Edit(Person person)
{
    ModelState.Remove(nameof(Person.Id));

    if (ModelState.IsValid)
       {
           //Save Changes;
       }
    }
}

or

public ActionResult Index([Bind(Exclude = nameof(SomeDomainModel.Id))] SomeDomainModel model)
寄居者 2024-10-17 14:45:40

正如德斯蒙德所说,我发现删除非常容易使用,而且我还做了一个简单的扩展,可以派上用场,可以忽略多个道具...

    /// <summary>
    /// Excludes the list of model properties from model validation.
    /// </summary>
    /// <param name="ModelState">The model state dictionary which holds the state of model data being interpreted.</param>
    /// <param name="modelProperties">A string array of delimited string property names of the model to be excluded from the model state validation.</param>
    public static void Remove(this ModelStateDictionary ModelState, params string[] modelProperties)
    {
        foreach (var prop in modelProperties)
            ModelState.Remove(prop);
    }

您可以在操作方法中像这样使用它:

    ModelState.Remove(nameof(obj.ID), nameof(obj.Prop2), nameof(obj.Prop3), nameof(obj.Etc));

As Desmond stated, I find remove very easy to use, also I've made a simple extension which can come in handy for multiple props to be ignored...

    /// <summary>
    /// Excludes the list of model properties from model validation.
    /// </summary>
    /// <param name="ModelState">The model state dictionary which holds the state of model data being interpreted.</param>
    /// <param name="modelProperties">A string array of delimited string property names of the model to be excluded from the model state validation.</param>
    public static void Remove(this ModelStateDictionary ModelState, params string[] modelProperties)
    {
        foreach (var prop in modelProperties)
            ModelState.Remove(prop);
    }

You can use it like this in your action method:

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