Мое приложение смоделировано после того, как NerdDinner представлен в http://www.asp.net/mvc/learn/. У меня возникли проблемы с сохранением моего опубликованного редактирования и созданием страниц, даже если ни одно из бизнес-правил в следующем классе Helper не нарушено. Единственное место, о котором я могу подумать, это то, что внешние ключи таблицы базы данных нарушены. Может ли кто-нибудь помочь мне поймать исключение базы данных? Спасибо.
[Bind(Include = "TransfersID,AdmitDate,AdmitTime,MedDate,MedTime,Hospital,Ward,WardPhone," +
"CaseMgrName,CaseMgrPhone,MDName,MDPhone,MDPager,AdmitDX,BedType,CallerName,CallerNumber," +
"AcceptBy,ListStatus,StatusReport,FeePay,TXFRPriority,InfectionType,OffListReason,OffListDate," +
"OffList,EnteredBy,VID,WardExtension,MDExtension,CaseMgrExtension,AddedToListDate," +
"OffListTime,TxSource,TxReason,InpatientUnit,CBOC,CLC_ASIH,CNH,OtherServicesNA,BackToHomeFacility")]
public partial class Transfer
{
public bool IsValid
{
get { return (GetRuleViolations().Count() == 0); }
}
public IEnumerable<RuleViolation> GetRuleViolations()
{
if (AdmitDate == null)
yield return new RuleViolation("Admit date is required", "AdmitDate");
if (Hospital == 0)
yield return new RuleViolation("Hospital is required", "Hospital");
if (String.IsNullOrEmpty(AdmitDX))
yield return new RuleViolation("Admit DX is required", "AdmitDX");
if (BedType == 0)
yield return new RuleViolation("Bed Type is required", "BedType");
if (ListStatus == 0)
yield return new RuleViolation("List Status is required", "ListStatus");
yield break;
}
partial void OnValidate(ChangeAction action)
{
if (!IsValid)
throw new ApplicationException("Rule violations prevent saving");
}
}
Контроллер имеет следующий метод.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, Transfer collection)
{
Transfer transfer = vRepository.GetTransfer(id);
if (transfer == null)
return View("NotFound");
else
{
try
{
UpdateModel<Transfer>(transfer);
if (transfer.FeePay == 0) transfer.FeePay = null;
if (transfer.InfectionType == 0) transfer.InfectionType = null;
if (transfer.OffListReason == 0) transfer.OffListReason = null;
if (transfer.TxSource == 0) transfer.TxSource = null;
if (transfer.TxReason == 0) transfer.TxReason = null;
if (transfer.OffListDate.HasValue) transfer.OffList = true;
if (!transfer.OffListDate.HasValue) transfer.OffList = false;
transfer.EnteredBy = HttpUtility.HtmlEncode(transfer.EnteredBy);
transfer.AdmitDX = HttpUtility.HtmlEncode(transfer.AdmitDX);
transfer.Ward = HttpUtility.HtmlEncode(transfer.Ward);
transfer.WardPhone = HttpUtility.HtmlEncode(transfer.WardPhone);
transfer.MDName = HttpUtility.HtmlEncode(transfer.MDName);
transfer.MDPhone = HttpUtility.HtmlEncode(transfer.MDPhone);
transfer.MDPager = HttpUtility.HtmlEncode(transfer.MDPager);
transfer.CallerName = HttpUtility.HtmlEncode(transfer.CallerName);
transfer.CallerNumber = HttpUtility.HtmlEncode(transfer.CallerNumber);
transfer.CaseMgrName = HttpUtility.HtmlEncode(transfer.CaseMgrName);
transfer.CaseMgrPhone = HttpUtility.HtmlEncode(transfer.CaseMgrPhone);
transfer.TXFRPriority = HttpUtility.HtmlEncode(transfer.TXFRPriority);
transfer.AcceptBy = HttpUtility.HtmlEncode(transfer.AcceptBy);
transfer.StatusReport = HttpUtility.HtmlEncode(transfer.StatusReport);
transfer.CBOC = HttpUtility.HtmlEncode(transfer.CBOC);
transfer.CLC_ASIH = HttpUtility.HtmlEncode(transfer.CLC_ASIH);
transfer.CNH = HttpUtility.HtmlEncode(transfer.CNH);
transfer.OtherServicesNA = HttpUtility.HtmlEncode(transfer.OtherServicesNA);
transfer.EnteredBy = HttpContext.User.Identity.Name;
vRepository.Save();
return RedirectToAction("Details", new { id = transfer.TransfersID });
}
catch
{
ModelState.AddModelErrors(transfer.GetRuleViolations());
return View(new TransferFormViewModel(transfer));
}
}
}
}
**** Обе страницы используют общую форму в пользовательском контроле. ****
<%@ Control Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<VS.Controllers.TransferFormViewModel>" %>
<%
var thisTransfer = Model.Transfer;
var thisVS = thisTransfer.VS;
%>
<%= Html.ValidationSummary("Please correct the errors and try again.") %>
<% using (Html.BeginForm()) { %>
<fieldset>
<legend>Transfer for
<%
if (thisVS != null)
Response.Write(thisVS.FirstName + ' ' + thisVS.LastName);
%>
<%
if (Model.Transfer.OffList.HasValue)
{
if ((bool)Model.Transfer.OffList)
Response.Write(" (Off List)");
else
Response.Write(" (On List)");
}
%>
</legend>
<div class="sideBySideDiv">
<p>
<label for="TransferID">Transfer ID:</label>
<%= thisTransfer.TransfersID %>
</p>
<p>
<label for="VID">VID:</label>
<% if (thisVS!=null) Response.Write(thisVS.VID); %>
</p>
<p>
<label for="LastFourSSN">SSN:</label>
<% if (thisVS!= null) Response.Write(thisVS.LastFourSSN);%>
</p>
<p>
<label for="DOB">DOB:</label>
<% if (thisVS!= null) Response.Write(String.Format("{0:d}", thisVS.DOB));%>
</p>
<p>
<label for="_SC">%SC:</label>
<% if (thisVS!= null) Response.Write(thisVS.PercentSC._SC);%>
</p>
<p>
<label for="PCP">PCP:</label>
<% if (thisVS!=null) Response.Write(thisVS.PCP); %>
</p>
<p>
<label for="Insurance">Insurance:</label>
<% if (thisVS!= null) Response.Write(string.IsNullOrEmpty(thisVS.Insurance) ? "NA" : thisVS.Insurance); %>
</p>
<p>
<label for="COJ">COJ:</label>
<% if (thisVS!= null) Response.Write(thisVS.Hospital.Hospital1); %>
</p>
<p><label for="TransportElig">Eligibility:</label>
<%if (thisVS!= null) Response.Write(thisVS.TransportElig ? "Yes" : "No"); %>
</p>
<p>
<label for="TxID">Transfer Record ID:</label>
<%= thisTransfer.TransfersID %>
<%= Html.ValidationMessage("TxID", "*") %>
</p>
<p>
<label for="Hospital">Hospital:</label><br />
<%= Html.DropDownList("Hospital", Model.CHospitals)%>
</p>
<p>
<label for="AdmitDX">Admit DX:</label>
<%= Html.TextBox("AdmitDX", thisTransfer.AdmitDX)%>
</p>
<p>
<label for="BedType">Bed Type:</label>
<%= Html.DropDownList("BedType", Model.BedTypes)%>
</p>
<p>
<label for="AdmitDate">Admit Date:</label>
<%=Html.TextBox("AdmitDate", string.Format("{0:d}", thisTransfer.AdmitDate))%>
</p>
<p>
<label for="ListStatus">List Status</label>
<%= Html.DropDownList("ListStatus", Model.StatusTypes)%>
</p>
<p>
<label for="Ward">Ward:</label>
<%= Html.TextBox("Ward", thisTransfer.Ward)%>
</p>
<p>
<label for="WardPhone">Ward Phone</label>
<%= Html.TextBox("WardPhone", thisTransfer.WardPhone)%>
</p>
<p>
<label for="MDName">Referred MD:</label>
<%= Html.TextBox("MDName", thisTransfer.MDName)%>
</p>
<p>
<label for="MDPhone">MD Phone:</label>
<%= Html.TextBox("MDPhone", thisTransfer.MDPhone)%>
</p>
<p>
<label for="MDPager">MD Pager:</label>
<%= Html.TextBox("MDPager", thisTransfer.MDPager)%>
</p>
<p>
<label for="AdmitTime">Admit Time:</label>
<%= Html.TextBox("AdmitTime", string.Format("{0:t}",thisTransfer.AdmitTime))%>
</p>
<p>
<label for="CallerName">Caller Name:</label>
<%=Html.TextBox("CallerName", thisTransfer.CallerName)%>
</p>
<p>
<label for="CallerNumber">Caller Number:</label>
<%= Html.TextBox("CallerNumber", thisTransfer.CallerNumber)%>
</p>
</div>
<div class="sideBySideDiv">
<p>
<label for="CaseMgrName">Case Mgr:</label>
<%= Html.TextBox("CaseMgrName", thisTransfer.CaseMgrName)%>
</p>
<p>
<label for="CaseMgrPhone">Mgr Phone:</label>
<%= Html.TextBox("CaseMgrPhone", thisTransfer.CaseMgrPhone)%>
</p>
<p>
<label for="MedDate">Med Date:</label>
<%= Html.TextBox("MedDate", string.Format("{0:d}",thisTransfer.MedDate))%>
<%= Html.ValidationMessage("MedDate", "*") %>
</p>
<p>
<label for="MedTime">Med Time:</label>
<%= Html.TextBox("MedTime", string.Format("{0:t}",thisTransfer.MedTime))%>
<%= Html.ValidationMessage("MedTime", "*") %>
</p>
<p>
<label for="TXFRPriority">TXFR Priority:</label>
<%= Html.TextBox("TXFRPriority", thisTransfer.TXFRPriority)%>
<%= Html.ValidationMessage("TxPriority", "*") %>
</p>
<p>
<label for="AcceptBy">Accepted By:</label>
<%= Html.TextBox("AcceptBy", thisTransfer.AcceptBy)%>
<%= Html.ValidationMessage("AcceptBy", "*") %>
</p>
<p>
<label for="OffListReason">Off List Reason:</label>
<%= Html.DropDownList("OffListReason", Model.OffReasonTypes)%>
<%= Html.ValidationMessage("OffListReason", "*") %>
</p>
<p>
<label for="OffListDate">Off List Date:</label>
<%= Html.TextBox("OffListDate", string.Format("{0:d}", thisTransfer.OffListDate))%>
<%= Html.ValidationMessage("OffListDate", "*")%>
</p>
<p>
<label for="InfectionType">Infection Type:</label>
<%= Html.DropDownList("InfectionType", Model.InfectionTypes)%>
<%= Html.ValidationMessage("InfectionType", "*") %>
</p>
<p>
<label for="FeePay">Fee Pay:</label>
<%= Html.DropDownList("FeePay", Model.FeePayReasons) %>
<%= Html.ValidationMessage("FeePay", "*") %>
</p>
<p>
<label for="StatusReport">Status Report:</label>
<%= Html.TextArea("StatusReport", thisTransfer.StatusReport)%>
<%= Html.ValidationMessage("StatusReport", "*") %>
</p>
</div>
<div class="sideBySideDiv">
<p>
<label for="TxSource">Trnasfer Out Source:</label><br />
<%= Html.RadioButton("TxSource", "1", (thisTransfer.TxSource ?? 0) == 1 ? true : false, new { id = "TxSource_ED" })%>ED
<%= Html.RadioButton("TxSource", "2", (thisTransfer.TxSource ?? 0) == 2 ? true : false, new { id = "TxSource_EdPsych" })%>EdPsych
<br />
<%= Html.RadioButton("TxSource", "3", (thisTransfer.TxSource ?? 0) == 3 ? true : false, new { id = "TxSource_InpatientUnit" })%>Inpatient Unit
<%=Html.TextBox("InpatientUnit", thisTransfer.InpatientUnit)%>
<%= Html.ValidationMessage("InpatientUnit", "*") %>
<%= Html.RadioButton("TxSource", "4", (thisTransfer.TxSource ?? 0) == 4 ? true : false, new { id = "TxSource_CBOC" })%>CBOC
<%= Html.TextBox("CBOC", thisTransfer.CBOC)%>
<%= Html.ValidationMessage("CBOC", "*") %>
<%= Html.RadioButton("TxSource", "5", (thisTransfer.TxSource ?? 0) == 5 ? true : false, new { id = "TxSource_CLC" })%>CLC
<%= Html.TextBox("CLC_ASIH", thisTransfer.CLC_ASIH)%>
<%= Html.ValidationMessage("CLC_ASIH", "*") %>
<%= Html.RadioButton("TxSource", "6", (thisTransfer.TxSource ?? 0) == 6 ? true : false, new { id = "TxSource_CNH" })%>CNH
<%= Html.TextBox("CNH", thisTransfer.CNH)%>
<%= Html.ValidationMessage("CNH", "*")%>
<%= Html.ValidationMessage("TxOutSource", "*") %>
</p>
<br />
<p>
<label for="TxReason">Treansfer Out Reason:</label><br />
<%= Html.RadioButton("TxReason", "1", (thisTransfer.TxReason ?? 0) == 1 ? true : false, new { id = "TxReason_EDdivert" })%>EDdivert
<%= Html.RadioButton("TxReason", "2", (thisTransfer.TxReason ?? 0) == 2 ? true : false, new { id = "TxReason_Trauma" })%>Trauma
<br />
<%= Html.RadioButton("TxReason", "3", (thisTransfer.TxReason ?? 0) == 3 ? true : false, new { id = "TxReason_OBGYN" })%>OBGYN
<br />
<%= Html.RadioButton("TxReason", "4", (thisTransfer.TxReason ?? 0) == 4 ? true : false, new { id = "TxReason_BedTypeNA" })%>Bed Type Not Available
<br />
<%= Html.RadioButton("TxReason", "5", (thisTransfer.TxReason ?? 0) == 5 ? true : false, new { id = "TxReason_NonV" })%>Non V
<%= Html.RadioButton("TxReason", "6", (thisTransfer.TxReason ?? 0) == 6 ? true : false, new { id = "TxReason_NotEligible" })%>Not Eligible
<br />
<%= Html.RadioButton("TxReason", "7", (thisTransfer.TxReason ?? 0) == 7 ? true : false, new { id = "TxReason_OtherServicesNotAvailable" })%>Other Services Not Available
<%= Html.TextBox("OtherServiceNA", thisTransfer.OtherServicesNA)%>
<%= Html.ValidationMessage("OtherServiceNA", "*") %>
<%= Html.RadioButton("TxReason", "8", (thisTransfer.TxReason ?? 0) == 8 ? true : false, new { id = "TxReason_BackToHomeFacility" })%>Transfer Back to Home Facility
<%= Html.TextBox("BackToHomeFacility",thisTransfer.BackToHomeFacility) %>
<%= Html.ValidationMessage("BackToHomeFacility", "*") %>
<%= Html.ValidationMessage("TxOutReason", "*") %>
</p>
<p>
<label for="AddedToListDate">Added to List Date:</label>
<%= Html.TextBox("AddedToListDate", string.Format("{0:d}", thisTransfer.AddedToListDate))%>
<%= Html.ValidationMessage("AddedToListDate", "*")%>
</p>
</div>
</fieldset>
<p>
<input type="submit" value="Save" />
</p>
<% } %><!-- close using Html.BeginForm()-->