ValueProvider.GetValue 扩展方法

发布于 2024-08-29 13:52:03 字数 1300 浏览 6 评论 0原文

我有一个这样的模型;

public class QuickQuote
{
    [Required]
    public Enumerations.AUSTRALIA_STATES  state { get; set; }

    [Required]
    public Enumerations.FAMILY_TYPE familyType { get; set; }

正如你所看到的,这两个属性是枚举。

现在我想使用我自己的模型绑定器,因为我现在不想费心去了解。

所以我有;

public class QuickQuoteBinder : DefaultModelBinder
{

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        quickQuote = new QuickQuote();

        try
        {
            quickQuote.state = (Enumerations.AUSTRALIA_STATES)
                Enum.Parse(typeof(Enumerations.AUSTRALIA_STATES),
                bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".state").AttemptedValue);
        }
        catch {
            ModelState modelState = new ModelState();
            ModelError err = new ModelError("Required");
            modelState.Errors.Add(err);
            bindingContext.ModelState.Add(bindingContext.ModelName + ".state", modelState);
        }

问题是,对于每个属性,并且有堆,我需要执行整个 try catch 块。

我想我可能会做的是创建一个扩展方法,它可以为我完成整个块,我需要传入的是模型属性和枚举。

所以我可以做类似的事情;

quickQuote.state = bindingContext.ValueProvider.GetModelValue("state", ...) 等。

这可能吗?

I have a model like this;

public class QuickQuote
{
    [Required]
    public Enumerations.AUSTRALIA_STATES  state { get; set; }

    [Required]
    public Enumerations.FAMILY_TYPE familyType { get; set; }

As you can see the two proerties are enumerations.

Now I want to employ my own model binder for reasons that I won't bother getting into at the moment.

So I have;

public class QuickQuoteBinder : DefaultModelBinder
{

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        quickQuote = new QuickQuote();

        try
        {
            quickQuote.state = (Enumerations.AUSTRALIA_STATES)
                Enum.Parse(typeof(Enumerations.AUSTRALIA_STATES),
                bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".state").AttemptedValue);
        }
        catch {
            ModelState modelState = new ModelState();
            ModelError err = new ModelError("Required");
            modelState.Errors.Add(err);
            bindingContext.ModelState.Add(bindingContext.ModelName + ".state", modelState);
        }

The problem is that for each property, and there are heaps, I need to do the whole try catch block.

What I thought I might do is create an extension method which would do the whole block for me and all i'd need to pass in is the model property and the enumeration.

So I could do something like;

quickQuote.state = bindingContext.ValueProvider.GetModelValue("state", ...) etc.

Is this possible?

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

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

发布评论

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

评论(2

我早已燃尽 2024-09-05 13:52:03

是的,你可以有一个扩展方法。这是一个非常简单的示例,展示了如何编写它。

public static class Extensions
{
    public static ValueProviderResult GetModel(this IValueProvider valueProvider, string key)
    {
        return valueProvider.GetValue(key);

    }
}

我要考虑的另一件事是使用 Enum.IsDefined 而不是 try catch 块。它将提高性能并可能产生更具可读性的代码。

Yes you can have an extension method. Here's a very simple example to show how you'd write it.

public static class Extensions
{
    public static ValueProviderResult GetModel(this IValueProvider valueProvider, string key)
    {
        return valueProvider.GetValue(key);

    }
}

The other thing I'd consider is to use Enum.IsDefined rather than a try catch block. It will improve performance and probably result in more readable code.

仅此而已 2024-09-05 13:52:03

好的,我明白了。

public static class TryGetValueHelper
{
    public static TEnum TryGetValue<TEnum>(this ModelBindingContext context, string property)
    {
        try
        {
            TEnum propertyValue = (TEnum)Enum.Parse(typeof(TEnum), context.ValueProvider.GetValue(property).AttemptedValue);
            return propertyValue;
        }
        catch {
            ModelState modelState = new ModelState();
            ModelError modelError = new ModelError("Required");
            modelState.Errors.Add(modelError);
            context.ModelState.Add(context.ModelName + "." + property, modelState);
        }

        return default(TEnum);

    }
}

's ok, I got it.

public static class TryGetValueHelper
{
    public static TEnum TryGetValue<TEnum>(this ModelBindingContext context, string property)
    {
        try
        {
            TEnum propertyValue = (TEnum)Enum.Parse(typeof(TEnum), context.ValueProvider.GetValue(property).AttemptedValue);
            return propertyValue;
        }
        catch {
            ModelState modelState = new ModelState();
            ModelError modelError = new ModelError("Required");
            modelState.Errors.Add(modelError);
            context.ModelState.Add(context.ModelName + "." + property, modelState);
        }

        return default(TEnum);

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