类库,MVC和数据注释

时间:2012-07-05 18:26:49

标签: asp.net-mvc

我是MVC的新手,并且不确定正确的设计。

我有类对象,我在各种应用程序中使用它。我已经采用这种方法编写自定义视图模型类,以便我可以访问所有这些对象中的属性并具有强类型。如果不在视图模型中重新键入所有类代码,是否有任何方法可以使用数据注释验证这些对象中的属性?如果我的方法和设计都错了,请告诉我。

[Required]        
public User user = new User("username");
//User has lots properites and methods, could i validate inside my class code?

//我想避免的是将以下内容放在我的自定义视图模型类中,//因为我已经有了一个包含这些东西的类库:

public class User
{

    [Required]
    [StringLength(160)]
    public string prop1 { get; set; }
    [Required]
    [StringLength(160)]
    public string prop2 { get; set; }
    [Required]
    [StringLength(160)]
    public string prop3 { get; set; }

    public User(string token)
    {
        SetUser(token);
    }


    public void SetUser(string token)
    {
        this.prop1 = "this";
        this.prop2 = "this2";
        this.prop3 = "this3";

    }

============ 很高兴知道我可以,但我在一些问题上遇到了磕磕绊绊。在我看来,我有:@ Html.EditorFor(modelItem => modelItem.user.prop1)

我将数据注释内容放在我的类域中。当它呈现它确实显示annoations。

<input class="text-box single-line" data-val="true" data-val-length="The field prop1 must be a string with a maximum length of 5." data-val-length-max="5" data-val-required="The prop1 field is required." id="user_prop1" name="user.prop1" type="text" value="somevalue" />

但是当我进入我的控制器时,参数为null。我认为因为名称是user.prop1。我尝试了一个文本框,我在其中指定了name属性,但我的控制器仍无法为我的参数获取值。

====================

            @model TrainingCalendar.Models.Training

            @{
                ViewBag.Title = "Signup";
            }

            <h2>Signup</h2>

            <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
            <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

            @using (Html.BeginForm("ConfirmSignup", "Training", FormMethod.Post))
            {
                @Html.ValidationSummary(true)
                <fieldset>
                    <legend>Training</legend>
                    <p>
                    @Html.Label("date", Model.SpecifiedCourse.strClassDate)
                    </p>
                    <p>
                    @Html.Label("time", Model.SpecifiedCourse.Time)
                    </p>
                    <p>
                    @Html.Label("instructor", Model.SpecifiedCourse.Instructor)
                    </p>
                    <p>
                    @Html.Hidden("id", Model.SpecifiedCourse.ID)
                    </p>
                    <table>
                        <tr>
                            <td>@Html.LabelFor(modelItem => modelItem.ApplicationUser.prop1)</td>
                            <td>@Html.EditorFor(modelItem => modelItem.ApplicationUser.prop1)</td>
                            <td style="color:Red">@Html.ValidationMessageFor(modelItem => modelItem.ApplicationUser.prop1)</td>
                        </tr>
                        <tr>
                            <td>@Html.LabelFor(modelItem => modelItem.ApplicationUser.prop2)</td>
                            <td>@Html.EditorFor(modelItem => modelItem.ApplicationUser.prop2)</td>
                            <td style="color:Red">@Html.ValidationMessageFor(modelItem => modelItem.ApplicationUser.prop2)</td>
                        </tr>

                    </table>   
                    <p>
                        <input type="submit" value="Sign Up" />
                    </p>
                </fieldset>
            }

            <div>
                @Html.ActionLink("Back to List", "Index")
            </div>

===================

            public ActionResult ConfirmSignup(
                        int id,
                        string prop1,
                        string prop2)
                    {
                        SignUpForClass();
                    return View();
                }

2 个答案:

答案 0 :(得分:0)

绝对可以在类代码上拥有数据注释属性。如果要在视图模型中封装类,请在视图中填充封装类的属性。在验证过程中,将根据您在类声明中指定的数据注释属性验证类成员。

答案 1 :(得分:0)

我偶然发现了这一点,我最终做了什么(我不知道它是最好的还是最正确的)也有数据注释主要影响我的类库的DOM中的数据库,例如MaxLength ,必需等,然后在我的视图模型中,我有主要与验证相关的数据注释,例如正则表达式,日期时间格式,最大值或最大长度。通过这种方式,我将系统的两个不同方面的角色分开。视图模型是从我的DOM到我的View可以使用的格式的转换,我的DOM专门用于定义数据在数据库中的外观。