如何从WCF服务/存储过程模型获取数据注释?

时间:2017-10-18 18:16:15

标签: asp.net-mvc wcf razor data-annotations

我有一个带有映射实体数据模型的WCF应用程序。数据注释未按预期工作。

填写表格的结果:

User            BirthDate               Savings
John Smith      1978-02-23 00:00:00     342
Susan Erikssen  1983-01-30 00:00:00     7323

这是源代码:

调用存储过程以获取用户信息:

public class Users : IUsers
{
    public List<sp_Get_Users> GetUsers()
    {
        var db = new DbEntities();
        return db.sp_Get_Users().ToList();
    }    
}

服务合同:

[ServiceContract]
public interface IUsers
{
    [OperationContract]
    List<sp_Get_Users> GetUsers();
}

存储过程模型,如您所见,我添加了一些数据注释:

using System.ComponentModel.DataAnnotations;
public partial class sp_Get_Users
{
    [Display(Name = "User Id")]
    public int User { get; set; }

    [DataType(DataType.Date)]
    public Nullable<System.DateTime> BirthDate { get; set; }

    [DataType(DataType.Currency)]
    public Nullable<decimal> Savings { get; set; }
}

控制器:

public class UsersController : Controller
{   
    public ActionResult Index()
    {
        var svc = new WcfSvc.UsersClient(); 
        return View(svc.GetUsers());
    }
}

Razor观点:

@model IEnumerable<Project.WcfSvc.sp_Get_Users_Result>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.User)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.BirthDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Savings)
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.User)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BirthDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Savings)
        </td>
    </tr>
}
</table>

0 个答案:

没有答案