使用默认值注释模型上的属性

发布于 2024-11-25 14:25:55 字数 618 浏览 1 评论 0原文

我创建了一个 EF4.1 代码优先模型(可能重要也可能不重要),并且我正在尝试获取“创建脚手架”模板的默认值。我的模型看起来像:

class Person {
    [DefaultValue (18)]
    public int Age { get; set; }
}

然后我的创建视图看起来像:

<div class="editor-label">
    @Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Age)
    @Html.ValidationMessageFor(model => model.Age)
</div>

我希望在运行时,EditorFor 会用“18”预先填充文本框,但它没有这样做。我是否误解了 DefaultValue 属性的用途,或者我还应该做其他事情?

注意:我不想在 EditorFor 方法上使用 new { Value = "18" } 重写,它似乎破坏了 DRY。

I created an EF4.1 code-first model (may or may not be important), and I'm trying to get default values for my Create scaffold template. My model looks like:

class Person {
    [DefaultValue (18)]
    public int Age { get; set; }
}

And then my Create view looks like:

<div class="editor-label">
    @Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Age)
    @Html.ValidationMessageFor(model => model.Age)
</div>

I would expect at runtime, that the EditorFor would pre-populate the textbox with "18", but it does no such thing. Am I misunderstanding what the DefaultValue attribute is for, or is there something else I should be doing?

Note: I don't want to use the new { Value = "18" } override on the EditorFor method, it seems to break DRY.

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

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

发布评论

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

评论(5

紧拥背影 2024-12-02 14:25:55

我不知道这是否能满足您的 DRY 需求,但我认为这是一个开始。

我会像这样重新设计模型:

public class Person {
    private const int DEFAULT_AGE = 18;
    private int _age = DEFAULT_AGE;
    [DefaultValue(DEFAULT_AGE)]
    public int Age {
        get { return _age; }
        set { _age = value; }
    }
}

保持视图不变,但在创建操作中执行以下操作:

public ActionResult Create() {
    return View(new Person());
}

这样,将使用默认的年龄值创建输入文本框,并且只有一个地方会使用默认值被指定。

I don't know if this will satisfy your DRY needs, but it's a start I think.

I would rework the model a bit like this:

public class Person {
    private const int DEFAULT_AGE = 18;
    private int _age = DEFAULT_AGE;
    [DefaultValue(DEFAULT_AGE)]
    public int Age {
        get { return _age; }
        set { _age = value; }
    }
}

Keep the view as is, but in the create action do this:

public ActionResult Create() {
    return View(new Person());
}

That way, the input textbox will be created with the default Age value, and there will be only one place where that default will be specified.

忆离笙 2024-12-02 14:25:55
class Person {
    public Person() {
        Age = 18;    
    }
    public int Age { get; set; }
}

在这种情况下,每次创建新的 Person 时,年龄都会被初始化为 18,即使新对象是由 Model Binder 创建的。

class Person {
    public Person() {
        Age = 18;    
    }
    public int Age { get; set; }
}

In this scenario, every time you do a new Person age will be initialized with 18 as age, even when the new object is created by the Model Binder.

月下凄凉 2024-12-02 14:25:55

模型

class Person {
    public Person() {
        Age = 18;    
    }
    public int Age { get; set; }
}

控制器

public ActionResult Create() {
    return View(new Person());
}

工作正常。

Model

class Person {
    public Person() {
        Age = 18;    
    }
    public int Age { get; set; }
}

Controller

public ActionResult Create() {
    return View(new Person());
}

It works fine.

独﹏钓一江月 2024-12-02 14:25:55

假设您的视图具有如下定义: -

@model Person

并且您的控制器 HTML GET 返回一个空视图

return View();

然后只需添加一个可以呈现的包含默认值的类

return View(new Person{ Age = 18 });

另一种选择是向您的 Person 类添加一个返回默认值的单例静态帮助器,填充类

  static public Person GetDefaultNew()
        {
            return new Person{ Age = 18 };
        }

那么你需要

return View(new Person.GetDefaultNew());

Assuming that your view has a definition such as:-

@model Person

and your controllers HTML GET returns an empty View

return View();

Then simply add a class that can be rendered that contains default values

return View(new Person{ Age = 18 });

Another option is to add a singleton static helper to your Person class that returns a default, populated class

  static public Person GetDefaultNew()
        {
            return new Person{ Age = 18 };
        }

Then you need

return View(new Person.GetDefaultNew());
泛滥成性 2024-12-02 14:25:55

设置 JSON.net 的 DefaultValueHandling 参数使 DefaultValue 起作用:

class Person {
    [JsonProperty("Age", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
    [DefaultValue(18)]
    public int Age { get; set; }
}

Setting JSON.net's DefaultValueHandling parameter makes DefaultValue work:

class Person {
    [JsonProperty("Age", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
    [DefaultValue(18)]
    public int Age { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文