2017-03-03 59 views
0

我正在學習MVC,爲樂譜庫存系統開發ASP.NET MVC項目,並且有兩個表:ScoreList和KeySignature。 KeySignature表具有主鍵(Id)和KeySignatureName。我還有另一張表,ScoreListItems,但我認爲這與我的問題無關。如何將子模型/表的主鍵保存爲父模型/表中的外鍵?

我想弄清楚如何做幾件事情。

1)當我創建一個新的ScoreList記錄時,我希望KeySignature字段是一個下拉列表,它從KeySignature表中填充KeySignatureNames。

2)當我保存記錄時,我想讓ScoreList表存儲KeySignatureId。

當我寫這篇文章聽起來應該很容易,但我一直在抓我的頭,並且無法弄清楚。任何幫助,將不勝感激。

這是我到目前爲止有:

主要簽名型號:

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace CreswellSolution.Model 
{ 
    public class KeySignature 
    { 
     public int KeySignatureId { get; set; } 
     public string KeySignatureName { get; set; } 

    } 
} 

ScoreListModel:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace CreswellSolution.Model 
{ 
    public class ScoreList : IObjectWithState 
    { 
     public ScoreList() 
     { 
      ScoreListItems = new List<ScoreListItem>(); 
     } 

     public int ScoreListId { get; set; } 
     public string ScoreListName { get; set; } 
     public string ScoreListComposer { get; set; } 
     public int KeySignatureId { get; set; } 

     public virtual List<ScoreListItem> ScoreListItems { get; set; } 

     public ObjectState ObjectState { get; set; } 
    } 
} 

ScoreListViewModel:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using CreswellSolution.Model; 

namespace CreswellSolution.Web.ViewModels 
{ 
    public class ScoreListViewModel : IObjectWithState 
    { 
     public ScoreListViewModel() 
     { 
      ScoreListItems = new List<ScoreListItemViewModel>(); 
      ScoreListItemsToDelete = new List<int>(); 
     } 

     public int ScoreListId { get; set; } 
     public string ScoreListName { get; set; } 
     public string ScoreListComposer { get; set; } 
     public int KeySignatureId { get; set; } 

     public List<ScoreListItemViewModel> ScoreListItems { get; set; } 

     public List<int> ScoreListItemsToDelete { get; set; } 

     public string MessageToClient { get; set; } 

     public ObjectState ObjectState { get; set; } 
    } 
} 

助手查看模式:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using CreswellSolution.Model; 

namespace CreswellSolution.Web.ViewModels 
{ 
    public static class Helpers 
    { 
     public static ScoreListViewModel CreateScoreListViewModelFromScoreList(ScoreList scoreList) 
     { 
      ScoreListViewModel scoreListViewModel = new ScoreListViewModel(); 
      scoreListViewModel.ScoreListId = scoreList.ScoreListId; 
      scoreListViewModel.ScoreListName = scoreList.ScoreListName; 
      scoreListViewModel.ScoreListComposer = scoreList.ScoreListComposer; 
      scoreListViewModel.KeySignatureId = scoreList.KeySignatureId; 
      scoreListViewModel.ObjectState = ObjectState.Unchanged; 

      foreach (ScoreListItem scoreListItem in scoreList.ScoreListItems) 
      { 
       ScoreListItemViewModel scoreListItemViewModel = new ScoreListItemViewModel(); 
       scoreListItemViewModel.ScoreListItemId = scoreListItem.ScoreListItemId; 
       scoreListItemViewModel.PartName = scoreListItem.PartName; 
       scoreListItemViewModel.TotalParts = scoreListItem.TotalParts; 
       scoreListItemViewModel.PartsCheckedOut = scoreListItem.PartsCheckedOut; 

       scoreListItemViewModel.ObjectState = ObjectState.Unchanged; 

       scoreListItemViewModel.ScoreListId = scoreList.ScoreListId; 

       scoreListViewModel.ScoreListItems.Add(scoreListItemViewModel); 
      } 

      return scoreListViewModel; 
     } 


     public static ScoreList CreateScoreListFromScoreListViewModel(ScoreListViewModel scoreListViewModel) 
     { 
      ScoreList scoreList = new ScoreList(); 
      scoreList.ScoreListId = scoreListViewModel.ScoreListId; 
      scoreList.ScoreListName = scoreListViewModel.ScoreListName; 
      scoreList.ScoreListComposer = scoreListViewModel.ScoreListComposer; 
      scoreList.KeySignatureId = scoreListViewModel.KeySignatureId; 
      scoreList.ObjectState = scoreListViewModel.ObjectState; 

      int temporaryScoreListItemId = -1; 

      foreach (ScoreListItemViewModel scoreListItemViewModel in scoreListViewModel.ScoreListItems) 
      { 
       ScoreListItem scoreListItem = new ScoreListItem(); 
       scoreListItem.PartName = scoreListItemViewModel.PartName; 
       scoreListItem.TotalParts = scoreListItemViewModel.TotalParts; 
       scoreListItem.PartsCheckedOut = scoreListItemViewModel.PartsCheckedOut; 

       scoreListItem.ObjectState = scoreListItemViewModel.ObjectState; 

       if (scoreListItemViewModel.ObjectState != ObjectState.Added) 
        scoreListItem.ScoreListItemId = scoreListItemViewModel.ScoreListItemId; 
       else 
       { 
        scoreListItem.ScoreListItemId = temporaryScoreListItemId; 
        temporaryScoreListItemId--; 
       } 

       scoreListItem.ScoreListId = scoreListViewModel.ScoreListId; 

       scoreList.ScoreListItems.Add(scoreListItem); 
      } 

      return scoreList; 
     } 

回答

0

首先,確保您的實體建模正確。他們應該像這樣的:

public class ScoreList 
{ 
    [Key] 
    public int Id { get; set; } 

    ... 

    [ForeignKey("KeySignature")] 
    public int KeySignatureId { get; set; } 
    public virtual KeySignature KeySignature { get; set; } 
} 

然後,在你的動作,你只需要得到一個IEnumerable<SelectListItem>與現有KeySignature選項:

var keySignatureOptions = db.KeySignatures.Select(m => new SelectListItem 
{ 
    Value = m.Id.ToString(), 
    Text = m.Name 
}); 

您可以此存儲ViewBag或最好,利用視圖模型並將其設置。

然後,在你的看法:

@Html.DropDownListFor(m => m.KeySignatureId, (IEnumerable<SelectListItem>)ViewBag.KeySignatureOptions) 

或者

@Html.DropDownListFor(m => m.KeySignatureId, Model.KeySignatureOptions) 

取決於哪個方向去的。

就是這樣。選定的值將回發給外鍵屬性,所以您只需保存,正常完成即可。

+0

非常感謝您的快速響應!我會在幾個小時內得到機會的時候嘗試一下。 – stradixx

+0

我用我已經有的一些代碼更新了我的問題。我正在使用ViewModels - 我有一個Helpers類和一個ScoreListViewModel類。我如何去存儲上面提到的keySignatureOptions變量? – stradixx

相關問題