我正在學習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;
}
非常感謝您的快速響應!我會在幾個小時內得到機會的時候嘗試一下。 – stradixx
我用我已經有的一些代碼更新了我的問題。我正在使用ViewModels - 我有一個Helpers類和一個ScoreListViewModel類。我如何去存儲上面提到的keySignatureOptions變量? – stradixx