Sal*_*mer 7 asp.net-mvc razor asp.net-mvc-3 asp.net-mvc-viewmodel
我刚刚开始使用ViewModels.你能查看一下这段代码,看看我是否遵循最佳做法?有什么不同寻常的吗?你会以不同的方式进行验证吗?
对不起,如果代码很长(有很多部分).我试图让它尽可能容易理解.
谢谢!
模型
public class CustomerModel
{
[Required(ErrorMessage="Primer nombre!")]
public string FirstName { get; set; }
[Required(ErrorMessage="Segundo nombre!")]
public string LastName { get; set; }
[Required(ErrorMessage="Edad")]
public int? Age { get; set; }
public string State { get; set; }
public string CountryID { get; set; }
[Required(ErrorMessage="Phone Number")]
public string PhoneNumber { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
视图模型
public class CustomerViewModel
{
public CustomerModel Customer { get; set; }
public string Phone1a { get; set; }
public string Phone1b { get; set; }
public string Phone1c { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
调节器
public ActionResult Index()
{
CustomerViewModel Customer = new CustomerViewModel()
{
Customer = new CustomerModel(),
};
return View(Customer);
}
[HttpPost]
public ActionResult Index(CustomerViewModel c)
{
//ModelState.Add("Customer.PhoneNumber", ModelState["Phone1a"]);
// Let's manually bind the phone number fields to the PhoneNumber properties in
// Customer object.
c.Customer.PhoneNumber = c.Phone1a + c.Phone1b + c.Phone1c;
// Let's check that it's not empty and that it's a valid phone number (logic not listed here)
if (!String.IsNullOrEmpty(c.Customer.PhoneNumber))
{
// Let's remove the fact that there was an error!
ModelState["Customer.PhoneNumber"].Errors.Clear();
} // Else keep the error there.
if (ModelState.IsValid)
{
Response.Write("<H1 style'background-color:white;color:black'>VALIDATED</H1>");
}
return View("Index", c);
}
}
Run Code Online (Sandbox Code Playgroud)
视图
@model MVVM1.Models.CustomerViewModel
@using (Html.BeginForm("Index", "Detail"))
{
<table border="1" cellpadding="1" cellspacing="1">
<tr>
<td>@Html.LabelFor(m => m.Customer.FirstName)</td>
<td>
@Html.TextBoxFor(m => m.Customer.FirstName)
@Html.ValidationMessageFor(m => m.Customer.FirstName)
</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.Customer.LastName)</td>
<td>
@Html.TextBoxFor(m => m.Customer.LastName)
@Html.ValidationMessageFor(m => m.Customer.LastName)
</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.Customer.Age)</td>
<td>
@Html.TextBoxFor(m => m.Customer.Age)
@Html.ValidationMessageFor(m => m.Customer.Age)
</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.Customer.PhoneNumber)</td>
<td width="350">
@Html.TextBoxFor(m => m.Phone1a, new { size="4", maxlength="3" })
@Html.TextBoxFor(m => m.Phone1b)
@Html.TextBoxFor(m => m.Phone1c)
<div>
@Html.ValidationMessageFor(m => m.Customer.PhoneNumber)
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Submit" /></td>
</tr>
</table>
}
Run Code Online (Sandbox Code Playgroud)
让我惊讶的一件事是:
if (ModelState.IsValid)
{
Response.Write("<H1 style'background-color:white;color:black'>VALIDATED</H1>");
}
return View("Index", c);
Run Code Online (Sandbox Code Playgroud)
请记住,视图模型非常适合将数据传递到控制器并返回到模型。我建议您将 IsValid 属性添加到视图模型中,然后将其设置为 true,而不是调用 Response.Write。然后只需将其添加到局部视图的顶部:
@if (Model.IsValid)
{
<H1 style'background-color:white;color:black'>VALIDATED</H1>
}
Run Code Online (Sandbox Code Playgroud)
在您看来,您也可以使用 ModelState,但有些人会认为这不是最佳实践。但是,如果您不想为模型添加只能在视图中看到的属性,则可以执行以下操作:
@if (ViewData.ModelState.IsValid)
Run Code Online (Sandbox Code Playgroud)
另一个挑剔的事情是 MVC 验证属性通常用于 UI 上的验证。此验证可以在其他领域重复使用,但在某些情况下不是最佳的。此外,您可能并不总是能够修改您的域模型。因此,为了将所有 UI 验证保留在一个位置,我通常将域模型包装在视图模型中,这样您会得到如下所示的结果:
public class CustomerViewModel
{
public CustomerModel Customer { get; set; }
[Required(ErrorMessage="Primer nombre!")]
public string FirstName
{
get { return Customer.FirstName; }
set { Customer.FirstName = value; }
}
...
Run Code Online (Sandbox Code Playgroud)
这可能看起来多余,并不总是值得付出努力,但在使用实体框架域模型或其他难以或无法修改的类时,这是一个值得考虑的好习惯。
归档时间: |
|
查看次数: |
1255 次 |
最近记录: |