【发布时间】:2011-10-29 22:50:49
【问题描述】:
我在带有 EntityFramework 4.1 的 ASP.Net 4.0 和带有 Razor 的 MVC3 中使用 WCF。
DataAnnotations 在以下情况下工作正常:
在另一个带有 EF 的 DLL 中拥有 POCO 类,然后使用来自我的表示层的直接调用填充模型。如果我尝试在前端网格上编辑电子邮件地址并故意放入虚假电子邮件,则该属性的 DataAnnotation 错误出现。
但是,如果我将方法与 WCF 中间层调用(而不是直接调用)一起使用,则数据会返回并得到很好的更新,除了 DataAnnotations不要在这种情况下工作。如果我尝试在前端网格上编辑电子邮件地址并故意放入虚假电子邮件,则该属性的 DataAnnotation 不会显示。
如何使用 WCF 让它工作?一定有某种 WCF 属性在我无法识别 DataAnnotations 的地方丢失。
下面是相关代码:
2 层场景:
型号
using System;
using System.Collections.Generic;
//using System.ServiceModel;
//using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions;
namespace YeagerTechModel
{
public class Customer
{
public Customer()
{
this.Projects = new HashSet<Project>();
}
public short CustomerID { get; set; }
[Required]
[StringLength(50)]
[DataType(DataType.EmailAddress)]
[Email]
public string Email { get; set; }
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string Company { get; set; }
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string FirstName { get; set; }
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string LastName { get; set; }
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string Address1 { get; set; }
[StringLength(50)]
[DataType(DataType.Text)]
public string Address2 { get; set; }
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string City { get; set; }
[StringLength(2, MinimumLength = 2, ErrorMessage = "Must have a length of 2.")]
[DataType(DataType.Text)]
public string State { get; set; }
[StringLength(10)]
[DataType(DataType.Text)]
[RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Zip")]
public string Zip { get; set; }
[StringLength(12)]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Invalid Phone")]
public string HomePhone { get; set; }
[StringLength(12)]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Invalid Phone")]
public string CellPhone { get; set; }
[StringLength(100)]
[DataType(DataType.Url)]
[Url]
public string Website { get; set; }
[StringLength(50)]
[DataType(DataType.EmailAddress)]
[Email]
public string IMAddress { get; set; }
public System.DateTime CreatedDate { get; set; }
public Nullable<System.DateTime> UpdatedDate { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
}
控制器:
[GridAction]
public ActionResult Index()
{
ViewData["ErrCode"] = string.Empty;
if (HttpContext.User.IsInRole("Admin"))
{
try
{
//IEnumerable<YeagerTechWcfService.Customer> customerList = db.GetCustomers();
//IEnumerable<YeagerTechModel.Customer> customerList = db.GetCustomers();
DbContext.Configuration.ProxyCreationEnabled = false;
IEnumerable<Customer> customerList = DbContext.Customers.Where(p => p.CustomerID > 0);
if (DbContext.Database.Connection != null)
{
if (DbContext.Database.Connection.State != System.Data.ConnectionState.Closed)
{
DbContext.Database.Connection.Close();
DbContext.Database.Connection.Dispose();
}
}
return View(new GridModel<YeagerTechModel.Customer>
{
Data = customerList
});
}
catch (Exception ex)
{
throw ex;
}
}
else
{
//HttpCookie cn = Request.Cookies["strCookieName"];
//if (cn != null)
//{
// YeagerTechWcfService.Customer cust = db.GetCustomerID(Convert.ToInt16(cn.Value),false);
// if (cust != null)
// {
// return View(new GridModel<YeagerTechWcfService.Customer>
// {
// //Data = cust
// });
// }
// else
// return View(new GridModel<YeagerTechWcfService.Customer>());
//}
//else
//{
// TempData["ErrCode"] = "CustView";
return RedirectToAction("Index", "Home");
//}
}
}
查看:
@model Telerik.Web.Mvc.GridModel<YeagerTechModel.Customer>
@{
ViewBag.Title = "Customer Index";
}
<h2>
Customer Index</h2>
@( Html.Telerik().Grid<YeagerTechModel.Customer>(Model.Data)
.Name("Customers")
.DataKeys(dataKeys => dataKeys.Add(o => o.CustomerID)
.RouteKey("CustomerID"))
.ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Text).ImageHtmlAttributes(new { style = "margin-left:0" }))
.Columns(columns =>
{
columns.Bound(o => o.CustomerID).Hidden(true);
columns.Command(commands =>
{
commands.Edit().ButtonType(GridButtonType.Text);
}).Width(200).Title("Command");
columns.Bound(o => o.Email).Width(200).Filterable(false);
columns.Bound(o => o.Company).Width(200).Filterable(false);
columns.Bound(o => o.FirstName).Width(100).Title("FName").Filterable(false);
columns.Bound(o => o.LastName).Width(100).Title("LName").Filterable(false);
columns.Bound(o => o.Address1).Width(200).Title("Addr1").Filterable(false).Sortable(false);
columns.Bound(o => o.Address2).Width(100).Title("Addr2").Filterable(false).Sortable(false);
columns.Bound(o => o.City).Width(100);
columns.Bound(o => o.State).Width(40).Title("ST");
columns.Bound(o => o.Zip).Width(60);
columns.Bound(o => o.HomePhone).Width(120).Filterable(false).Sortable(false);
columns.Bound(o => o.CellPhone).Width(120).Filterable(false).Sortable(false);
columns.Bound(o => o.Website).Width(100).Filterable(false).Sortable(false);
columns.Bound(o => o.IMAddress).Width(100).Filterable(false).Sortable(false);
columns.Bound(o => o.CreatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120).Filterable(false).Sortable(false);
columns.Bound(o => o.UpdatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120).Filterable(false).Sortable(false);
}).DataBinding(dataBinding =>
dataBinding.Ajax()
.Insert("_InsertAjaxEditing", "Customer")
.Update("_SaveAjaxEditing", "Customer"))
.Editable(editing => editing.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Filterable()
.Scrollable()
)
NTier 场景 型号:
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions;
namespace YeagerTechModel
{
[Serializable]
[DataContract(IsReference = true)]
public class Customer
{
public Customer()
{
this.Projects = new HashSet<Project>();
}
[DataMember]
public short CustomerID { get; set; }
[DataMember]
[Required]
[StringLength(50)]
[DataType(DataType.EmailAddress)]
[Email]
public string Email { get; set; }
[DataMember]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string Company { get; set; }
[DataMember]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string FirstName { get; set; }
[DataMember]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string LastName { get; set; }
[DataMember]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string Address1 { get; set; }
[DataMember]
[StringLength(50)]
[DataType(DataType.Text)]
public string Address2 { get; set; }
[DataMember]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string City { get; set; }
[DataMember]
[StringLength(2, MinimumLength = 2, ErrorMessage = "Must have a length of 2.")]
[DataType(DataType.Text)]
public string State { get; set; }
[DataMember]
[StringLength(10)]
[DataType(DataType.Text)]
[RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Zip")]
public string Zip { get; set; }
[DataMember]
[StringLength(12)]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Invalid Phone")]
public string HomePhone { get; set; }
[DataMember]
[StringLength(12)]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Invalid Phone")]
public string CellPhone { get; set; }
[DataMember]
[StringLength(100)]
[DataType(DataType.Url)]
[Url]
public string Website { get; set; }
[DataMember]
[StringLength(50)]
[DataType(DataType.EmailAddress)]
[Email]
public string IMAddress { get; set; }
[DataMember]
public System.DateTime CreatedDate { get; set; }
[DataMember]
public Nullable<System.DateTime> UpdatedDate { get; set; }
[DataMember]
public virtual ICollection<Project> Projects { get; set; }
}
}
WCF 调用:
public IEnumerable<Customer> GetCustomers()
{
YeagerTechEntities DbContext = new YeagerTechEntities();
DbContext.Configuration.ProxyCreationEnabled = false;
IQueryable<Customer> customer = DbContext.Customers.Where(p => p.CustomerID > 0);
CloseConnection(DbContext);
return customer;
}
控制器:
[GridAction]
public ActionResult Index()
{
ViewData["ErrCode"] = string.Empty;
if (HttpContext.User.IsInRole("Admin"))
{
try
{
IEnumerable<YeagerTechWcfService.Customer> customerList = db.GetCustomers();
return View(new GridModel<YeagerTechWcfService.Customer>
{
Data = customerList
});
}
catch (Exception ex)
{
throw ex;
}
}
else
{
return RedirectToAction("Index", "Home");
}
}
查看:
@model Telerik.Web.Mvc.GridModel<YeagerTech.YeagerTechWcfService.Customer>
@{
ViewBag.Title = "Customer Index";
}
<h2>
Customer Index</h2>
@( Html.Telerik().Grid<YeagerTech.YeagerTechWcfService.Customer>(Model.Data)
.Name("Customers")
.DataKeys(dataKeys => dataKeys.Add(o => o.CustomerID)
.RouteKey("CustomerID"))
.ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Text).ImageHtmlAttributes(new { style = "margin-left:0" }))
.Columns(columns =>
{
columns.Bound(o => o.CustomerID).Hidden(true);
columns.Command(commands =>
{
commands.Edit().ButtonType(GridButtonType.Text);
}).Width(200).Title("Command");
columns.Bound(o => o.Email).Width(200).Filterable(false);
columns.Bound(o => o.Company).Width(200).Filterable(false);
columns.Bound(o => o.FirstName).Width(100).Title("FName").Filterable(false);
columns.Bound(o => o.LastName).Width(100).Title("LName").Filterable(false);
columns.Bound(o => o.Address1).Width(200).Title("Addr1").Filterable(false).Sortable(false);
columns.Bound(o => o.Address2).Width(100).Title("Addr2").Filterable(false).Sortable(false);
columns.Bound(o => o.City).Width(100);
columns.Bound(o => o.State).Width(40).Title("ST");
columns.Bound(o => o.Zip).Width(60);
columns.Bound(o => o.HomePhone).Width(120).Filterable(false).Sortable(false);
columns.Bound(o => o.CellPhone).Width(120).Filterable(false).Sortable(false);
columns.Bound(o => o.Website).Width(100).Filterable(false).Sortable(false);
columns.Bound(o => o.IMAddress).Width(100).Filterable(false).Sortable(false);
columns.Bound(o => o.CreatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120).Filterable(false).Sortable(false);
columns.Bound(o => o.UpdatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120).Filterable(false).Sortable(false);
}).DataBinding(dataBinding =>
dataBinding.Ajax()
.Insert("_InsertAjaxEditing", "Customer")
.Update("_SaveAjaxEditing", "Customer"))
.Editable(editing => editing.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Filterable()
.Scrollable()
)
【问题讨论】:
标签: wcf asp.net-mvc-3 entity-framework-4.1