如何从界面获取价值?

时间:2015-06-01 08:40:42

标签: asp.net-mvc

我有:

public class UserContactInformation : IUserContactInformation
{
    public bool IsDefaultContactInformation { get; set; }
    public string Email { get; set; }
}

和界面:

public interface IUserContactInformation
{
    bool IsDefaultContactInformation { get; set; }
    string Email { get; set; }
    string Phone { get; set; }
}

如何在控制器中收到电子邮件,因为我在控制器中看不到UserContactInformation。它不可见。我需要一些像getEmail()这样的功能吗?我不能用这样的东西:

model.Email = user.UserContactInformation.Email;

2 个答案:

答案 0 :(得分:0)

界面:link

  

实现接口的类或结构必须实现接口定义

中指定的接口成员

所以,如果你想使用它,创建一个实例然后使用它:

var userContactInfo = new UserContactInformation();
model.Email = userContactInfo.Email;

答案 1 :(得分:0)

您的代码目前无效,无法编译,因为您无法在界面中定义Phone,然后无法在已定义的类中实现它。

public interface IUserContactInformation
{
    bool IsDefaultContactInformation { get; set; }
    string Email { get; set; }
    string Phone { get; set; }
}

// Invalid
public class UserContactInformation : IUserContactInformation
{
    public bool IsDefaultContactInformation { get; set; }
    public string Email { get; set; }
}

// Valid
public class UserContactInformation : IUserContactInformation
{
    public bool IsDefaultContactInformation { get; set; }
    public string Email { get; set; }

    // If this is in the interface, it needs to be implemented
    public string Phone { get; set; }
}

其次,如果您想打印Email,可能是在视图中,请在视图中设置模型,然后使用剃刀语法print设置模型:

<强> ExampleView.cshtml:

@model IUserContactInformation

<p>Hello, my email address is @Model.Email<p>