我正在使用ASP.NET MVC 2,Entity Framework v4,ADO.NET C#POCO实体生成器和Repository模式。很多流行语!
我有一个存储库, IPartiesRepository 。我有一个 Party 对象和一个* Party_Identification *对象。党是个人或组织的通用术语。
在SQL /物理模型级别,Party表具有单个主键PartyId。 Party_Identifications表有一个由三个字段组成的复合键 - PartyId,IdentificationTypeCode和IdentificationStartDate。
我有一个强类型(到 Party )视图,其中列出了Party信息和相关标识。用户可以编辑聚会的标识;编辑链接是通过ActionLink生成的,如下所示:
<h3>Identification Data</h3>
<% foreach (var Identification in Model.Party_Identifications) { %>
<%: Identification.IdentificationTypeCode %>: <%: Identification.IdentificationValue %> - <%: Html.ActionLink("Edit", "EditIdentification", new { partyId = Identification.PartyId, idTypeCode = Identification.IdentificationTypeCode, startDate = Identification.PartyIdentificationStartDate })%><br />
<% } %>
PartyController 中的 EditIdentification()方法如下所示:
public ActionResult EditIdentification(int partyId, string idTypeCode, string startDate)
{
var party = partiesRepository.GetPartyById(partyId);
var identification = party.Party_Identifications.Where(x => x.PartyId == partyId && x.IdentificationTypeCode == idTypeCode && x.PartyIdentificationStartDate == DateTime.Parse(startDate)).First();
return View("EditIdentification", identification);
}
EditIdentification 视图允许用户通过基本形式修改身份记录:
<% using (Html.BeginForm("EditIdentification", "Party")) { %>
<%: Html.EditorForModel()%>
<input type="submit" value="Save" />
<% } %>
我在PartyController中有另一个 EditIdentification()方法来处理保存。我试图看看我们是否正在更新现有的识别记录,或者创建一个新的识别记录(我还在 PartyController 中有 Create()方法返回<使用Party_Identification对象作为模型的em> EditIdentification 视图。
我知道有更好的方法来做我正在做的事情(特别是检测修改后的vs新记录,并更新修改后的记录) - 我该如何改进呢?
[HttpPost]
public ActionResult EditIdentification(Party_Identification partyidentification)
{
TryUpdateModel(partyidentification);
if (partyidentification.CreationDate == DateTime.Parse("01/01/0001"))
{
partyidentification.CreationDate = DateTime.Now;
partyidentification.CreatedByName = User.Identity.Name.ToString();
partyidentification.ModificationDate = DateTime.Now;
partyidentification.ModifiedByName = User.Identity.Name.ToString();
}
// TODO: Validation goes here...
if (ModelState.IsValid)
{
var party = partiesRepository.GetPartyById(partyidentification.PartyId) as Party;
int partyCount = party.Party_Identifications
.Where(x => x.PartyId == partyidentification.PartyId &&
x.IdentificationTypeCode == partyidentification.IdentificationTypeCode &&
x.PartyIdentificationStartDate == partyidentification.PartyIdentificationStartDate)
.Count();
if (partyCount == 0)
{
party.Party_Identifications.Add(partyidentification);
}
else
{
var idtoupdate = party.Party_Identifications
.Where(x => x.PartyId == partyidentification.PartyId &&
x.IdentificationTypeCode == partyidentification.IdentificationTypeCode &&
x.PartyIdentificationStartDate == partyidentification.PartyIdentificationStartDate)
.First();
idtoupdate.IdentificationValue = partyidentification.IdentificationValue;
idtoupdate.PartyIdentificationEndDate = partyidentification.PartyIdentificationEndDate;
idtoupdate.ModificationDate = DateTime.Now;
idtoupdate.ModifiedByName = User.Identity.Name.ToString();
}
partiesRepository.SaveParty(party);
return View("Edit", party);
}
else
{
return View("Edit", partyidentification);
}
}
答案 0 :(得分:0)
您似乎在重复不需要的代码:
if (ModelState.IsValid)
{
var party = partiesRepository.GetPartyById(partyidentification.PartyId) as Party;
int partyCount = party.Party_Identifications
.Where(x => x.PartyId == partyidentification.PartyId &&
x.IdentificationTypeCode == partyidentification.IdentificationTypeCode &&
x.PartyIdentificationStartDate == partyidentification.PartyIdentificationStartDate)
.first();
if (partyCount == null)
{
party.Party_Identifications.Add(partyidentification);
}
else
{
partyCount.IdentificationValue = partyidentification.IdentificationValue;
partyCount.PartyIdentificationEndDate = partyidentification.PartyIdentificationEndDate;
partyCount.ModificationDate = DateTime.Now;
partyCount.ModifiedByName = User.Identity.Name.ToString();
}
partiesRepository.SaveParty(party);
return View("Edit", party);
}
else
{
return View("Edit", partyidentification);
}