【发布时间】:2012-07-18 06:25:21
【问题描述】:
我是 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)
我把数据注释的东西放在我的类域中。当它渲染时,它会显示注解。
<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" />
但是当我转到我的控制器时,参数为空。我想是因为名字是user.prop1。我尝试了一个指定名称属性的文本框,但我的控制器仍然无法为我的参数获取值。
=====================
@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 个字符串、1 个整数和 2 个我自己创建的对象:用户和培训课程。用户对象有一个设置值的方法,但是,我已将其移出构造函数。任何帮助表示赞赏。
-
您能发布您的 POST(没有双关语)操作方法吗?
-
这是我的代码的简化版本。我只好拍了拍我的额头。当我创建一个新视图并为我的模型选择一个强类型视图时,我设法让验证工作。然后经过一些测试和增强,客户端验证停止工作。由于 MVC Asp.net 为您做了这么多,我无法追踪发生了什么变化。尝试创建另一个新视图并没有解决问题。并且 prop1 和 prop2 参数即将为空。
-
我注意到您正在使用一些单独的属性而不是您的视图模型作为您操作的参数。如果您的视图模型名为 Training,那么您的签名应该是:public ActionResult ConfirmSignup(Training trainingObject){ // code here }。然后 trainingObject 对象将由具有模型绑定的视图填充。您可以拥有可以通过各种方式传递的其他参数,但模型绑定允许您重新创建视图模型作为 POST 操作的参数,假设视图是强类型的视图模型。
标签: asp.net-mvc