簡體   English   中英

是否可以拆分下拉列表中選擇的全名並將其保存為2個單獨的列,分別作為名字和姓氏?

[英]Is it possible to split a full name selected in a dropdown list and save it into 2 separate columns as first name and last name?

我在“實體框架代碼”第一種方法中有以下幾列:

public string LastName { get; set; }
public string FirstName { get; set; }

然后將它們組合成一個全名:

 public string FullName
    {
        get { return LastName + " " + FirstName; }
    }

我什至不知道是否可能,但是當從例如下拉列表中選擇全名時,如何將其從全名發送到其他兩列呢?

比較簡單的方法是在看到的第一個空格上進行拆分,但是如果任何人的名字或姓氏都帶有空格,則該方法會中斷。 破損的名字很少見,但帶有空格的姓氏很常見,例如“ Jean-Claude Van Damme”。

 public String FullName {
     set {
         Int32 spaceIdx = value.IndexOf(" ");
         if( spaceIdx == -1 ) {
             this.FirstName = value;
             this.LastName  = "";
         }
         else
         {
             this.FirstName = value.Substring(0, spaceIdx);
             this.LastName  = value.Substring(spaceIdx + 1);
         }
     }
 }

更好的方法是在系統中唯一標識名稱,然后從名稱下拉列表中使用它們。 假設這是ASP.NET MVC:

public class YourViewModel {
    public IEnumerable<SelectListItem> Names { get; set; }
    public Int32 SelectedNameByPersonId { get; set; }
}

在您的控制器中:

public IActionResult YourAction() {

    List<SelectListItem> names = db.People.Select( dbPerson => new SelectListItem() {
        Text = dbPerson.FirstName + " " + dbPerson.LastName,
        Value = dbPerson.Id.ToString()
    } ).ToList(); // ToList so the DB is only queried once

    return this.View( new YourViewModel() { Names = names } );
}

[HttpPost]
public IActionResult YourAction(YourViewModel model) {

    DBPerson dbPerson = db.People.GetPerson( model.SelectedNameByPersonId );
    // do stuff with person
}

在您看來(aspx語法):

<%= Html.DropDownFor( m => m.SelectNameByPersonId, this.Model.Names ) %>

假設您要執行FullName.Split(''); 獲取名稱數組。 當名字和姓氏是單個單詞時,這一切都很好。 但是John Billy Doe呢? 姓氏在哪里結束,姓氏在哪里開始?

相反,您可以使用其他分隔符(例如逗號):John,Billy Doe。 這樣,執行FullName.Split(','); 將產生正確的姓氏和名字。

public string FullName
{
    get
    {
         return LastName + ", " + FirstName;
    }
    set
    {
         string[] names = value.Split(", ");
         LastName = names[0];
         FirstName = names[1];
    }
}

編輯:當然,該值需要一些驗證,但是要在Android應用程序上鍵入代碼非常困難(就像我正在做的那樣)。 因此,除非您需要幫助,否則請您自行決定。

我已經嘗試了一些解決方法。 請嘗試此操作,讓我知道它的工作原理。

    public string LastName { get; set; }
    public string FirstName { get; set; }

    [NotMapped] //This will make sure that entity framework will not map this to a new column
    public string FullName
    {
        get { return LastName + " " + FirstName; }
        // Here comes your desired setter method.
        set
        {
            string[] str = value.Split(' ');
            if (str.Length == 2)
            {
                FirstName = str[1];
                LastName = str[0];
            }
            else if (str.Length >= 0 && value.Length > 0)
            {
                LastName = str[0];
            }
            else
                throw new Exception("Invalid name");
        }
    }

我不確定您要問的是什么,但是我認為這可能會有所幫助(加上輸入錯誤處理)。

public string LastName { get; set; }
public string FirstName { get; set; }
public string FullName
{
    get
    {
        return LastName + " " FirstName;
    }

    series
    {
        var nameParts = value.Split(' ');
        LastName = string.IsNullOrEmpty(nameParts[0]) ? string.Empty : nameParts[0];
        FirstName = string.IsNullOrEmpty(nameParts[1]) ? string.Empty : nameParts[1];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM