简体   繁体   English

如何使用EditorFor在一个字段中使用多个属性?

[英]How can I use multi properties in one field using EditorFor?

I have a Customer class: 我有一个Customer类:

public class Customer 
{
    public int Id { get; set; }
    public string Name { get; set; }
    string Surname { get; set; }
}

How can I use Name and Surname together in one field using Html.EditoFor ? 如何使用Html.EditoFor一个字段中同时使用“ Name和“ Surname

@Html.EditorFor(model => model.Customer.Name + Surename???? , new { htmlAttributes = new { @class = "form-control" } })

You can try to add NameWithSurname property to connected Name and Surname value. 您可以尝试将NameWithSurname属性添加到连接的NameSurname值。

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }

    private string _NameWithSurname;

    public string NameWithSurname
    {
        get
        {
            _NameWithSurname = Name + Surname;
            return _NameWithSurname;
        }
        set {
            _NameWithSurname = value;
        }
    }
}
@Html.EditorFor(model => model.Customer.NameWithSurname  , new { htmlAttributes = new { @class = "form-control" } })

NOTE 注意

Your Surname property might be public , otherwise it only can use in Customer class. 您的Surname属性可能是public ,否则只能在Customer类中使用。

You should either have the name split or combined - don't try to do both. 您应该将名称分开或合并-请勿同时使用。

Suppose you do have a "full name" textbox, the value gets edited, and the user types Billy Bob Thornton . 假设您确实有一个“全名”文本框,值被编辑,并且用户键入Billy Bob Thornton How then will it know how to split back into the first and last name fields? 那么它将如何知道如何拆分回名字和姓氏字段?

You need to either use separate text boxes and properties, or use a single "name" property and a single textbox. 您需要使用单独的文本框和属性,或者使用单个“名称”属性和单个文本框。

It's fine to combine the names when displaying data, but when editing it's more trouble than it's worth. 显示数据时组合名称是可以的 ,但是在编辑时,麻烦多于其所值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM