Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC Unable to save data
    text
    copied!<p><strong>My application is modelled after NerdDinner presented in <a href="http://www.asp.net/mvc/learn/" rel="nofollow noreferrer">http://www.asp.net/mvc/learn/</a>. I am having problem saving my posted edit and create pages even none of the business rules in the following Helper class is violated. The only place that I can think of is the database table foreign keys are violated. Could someone help me to catch the database exception? Thanks.</strong></p> <pre><code>[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&lt;RuleViolation&gt; 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"); } } </code></pre> <p><strong>The controller has the following method.</strong></p> <pre><code> [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id, Transfer collection) { Transfer transfer = vRepository.GetTransfer(id); if (transfer == null) return View("NotFound"); else { try { UpdateModel&lt;Transfer&gt;(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)); } } } } </code></pre> <p>****Both pages use shared form in a user controll.****</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;VS.Controllers.TransferFormViewModel&gt;" %&gt; &lt;% var thisTransfer = Model.Transfer; var thisVS = thisTransfer.VS; %&gt; &lt;%= Html.ValidationSummary("Please correct the errors and try again.") %&gt; &lt;% using (Html.BeginForm()) { %&gt; &lt;fieldset&gt; &lt;legend&gt;Transfer for &lt;% if (thisVS != null) Response.Write(thisVS.FirstName + ' ' + thisVS.LastName); %&gt; &lt;% if (Model.Transfer.OffList.HasValue) { if ((bool)Model.Transfer.OffList) Response.Write(" (Off List)"); else Response.Write(" (On List)"); } %&gt; &lt;/legend&gt; &lt;div class="sideBySideDiv"&gt; &lt;p&gt; &lt;label for="TransferID"&gt;Transfer ID:&lt;/label&gt; &lt;%= thisTransfer.TransfersID %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="VID"&gt;VID:&lt;/label&gt; &lt;% if (thisVS!=null) Response.Write(thisVS.VID); %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="LastFourSSN"&gt;SSN:&lt;/label&gt; &lt;% if (thisVS!= null) Response.Write(thisVS.LastFourSSN);%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="DOB"&gt;DOB:&lt;/label&gt; &lt;% if (thisVS!= null) Response.Write(String.Format("{0:d}", thisVS.DOB));%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="_SC"&gt;%SC:&lt;/label&gt; &lt;% if (thisVS!= null) Response.Write(thisVS.PercentSC._SC);%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="PCP"&gt;PCP:&lt;/label&gt; &lt;% if (thisVS!=null) Response.Write(thisVS.PCP); %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Insurance"&gt;Insurance:&lt;/label&gt; &lt;% if (thisVS!= null) Response.Write(string.IsNullOrEmpty(thisVS.Insurance) ? "NA" : thisVS.Insurance); %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="COJ"&gt;COJ:&lt;/label&gt; &lt;% if (thisVS!= null) Response.Write(thisVS.Hospital.Hospital1); %&gt; &lt;/p&gt; &lt;p&gt;&lt;label for="TransportElig"&gt;Eligibility:&lt;/label&gt; &lt;%if (thisVS!= null) Response.Write(thisVS.TransportElig ? "Yes" : "No"); %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="TxID"&gt;Transfer Record ID:&lt;/label&gt; &lt;%= thisTransfer.TransfersID %&gt; &lt;%= Html.ValidationMessage("TxID", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Hospital"&gt;Hospital:&lt;/label&gt;&lt;br /&gt; &lt;%= Html.DropDownList("Hospital", Model.CHospitals)%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="AdmitDX"&gt;Admit DX:&lt;/label&gt; &lt;%= Html.TextBox("AdmitDX", thisTransfer.AdmitDX)%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="BedType"&gt;Bed Type:&lt;/label&gt; &lt;%= Html.DropDownList("BedType", Model.BedTypes)%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="AdmitDate"&gt;Admit Date:&lt;/label&gt; &lt;%=Html.TextBox("AdmitDate", string.Format("{0:d}", thisTransfer.AdmitDate))%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="ListStatus"&gt;List Status&lt;/label&gt; &lt;%= Html.DropDownList("ListStatus", Model.StatusTypes)%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Ward"&gt;Ward:&lt;/label&gt; &lt;%= Html.TextBox("Ward", thisTransfer.Ward)%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="WardPhone"&gt;Ward Phone&lt;/label&gt; &lt;%= Html.TextBox("WardPhone", thisTransfer.WardPhone)%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="MDName"&gt;Referred MD:&lt;/label&gt; &lt;%= Html.TextBox("MDName", thisTransfer.MDName)%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="MDPhone"&gt;MD Phone:&lt;/label&gt; &lt;%= Html.TextBox("MDPhone", thisTransfer.MDPhone)%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="MDPager"&gt;MD Pager:&lt;/label&gt; &lt;%= Html.TextBox("MDPager", thisTransfer.MDPager)%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="AdmitTime"&gt;Admit Time:&lt;/label&gt; &lt;%= Html.TextBox("AdmitTime", string.Format("{0:t}",thisTransfer.AdmitTime))%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="CallerName"&gt;Caller Name:&lt;/label&gt; &lt;%=Html.TextBox("CallerName", thisTransfer.CallerName)%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="CallerNumber"&gt;Caller Number:&lt;/label&gt; &lt;%= Html.TextBox("CallerNumber", thisTransfer.CallerNumber)%&gt; &lt;/p&gt; &lt;/div&gt; &lt;div class="sideBySideDiv"&gt; &lt;p&gt; &lt;label for="CaseMgrName"&gt;Case Mgr:&lt;/label&gt; &lt;%= Html.TextBox("CaseMgrName", thisTransfer.CaseMgrName)%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="CaseMgrPhone"&gt;Mgr Phone:&lt;/label&gt; &lt;%= Html.TextBox("CaseMgrPhone", thisTransfer.CaseMgrPhone)%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="MedDate"&gt;Med Date:&lt;/label&gt; &lt;%= Html.TextBox("MedDate", string.Format("{0:d}",thisTransfer.MedDate))%&gt; &lt;%= Html.ValidationMessage("MedDate", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="MedTime"&gt;Med Time:&lt;/label&gt; &lt;%= Html.TextBox("MedTime", string.Format("{0:t}",thisTransfer.MedTime))%&gt; &lt;%= Html.ValidationMessage("MedTime", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="TXFRPriority"&gt;TXFR Priority:&lt;/label&gt; &lt;%= Html.TextBox("TXFRPriority", thisTransfer.TXFRPriority)%&gt; &lt;%= Html.ValidationMessage("TxPriority", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="AcceptBy"&gt;Accepted By:&lt;/label&gt; &lt;%= Html.TextBox("AcceptBy", thisTransfer.AcceptBy)%&gt; &lt;%= Html.ValidationMessage("AcceptBy", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="OffListReason"&gt;Off List Reason:&lt;/label&gt; &lt;%= Html.DropDownList("OffListReason", Model.OffReasonTypes)%&gt; &lt;%= Html.ValidationMessage("OffListReason", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="OffListDate"&gt;Off List Date:&lt;/label&gt; &lt;%= Html.TextBox("OffListDate", string.Format("{0:d}", thisTransfer.OffListDate))%&gt; &lt;%= Html.ValidationMessage("OffListDate", "*")%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="InfectionType"&gt;Infection Type:&lt;/label&gt; &lt;%= Html.DropDownList("InfectionType", Model.InfectionTypes)%&gt; &lt;%= Html.ValidationMessage("InfectionType", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="FeePay"&gt;Fee Pay:&lt;/label&gt; &lt;%= Html.DropDownList("FeePay", Model.FeePayReasons) %&gt; &lt;%= Html.ValidationMessage("FeePay", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="StatusReport"&gt;Status Report:&lt;/label&gt; &lt;%= Html.TextArea("StatusReport", thisTransfer.StatusReport)%&gt; &lt;%= Html.ValidationMessage("StatusReport", "*") %&gt; &lt;/p&gt; &lt;/div&gt; &lt;div class="sideBySideDiv"&gt; &lt;p&gt; &lt;label for="TxSource"&gt;Trnasfer Out Source:&lt;/label&gt;&lt;br /&gt; &lt;%= Html.RadioButton("TxSource", "1", (thisTransfer.TxSource ?? 0) == 1 ? true : false, new { id = "TxSource_ED" })%&gt;ED &lt;%= Html.RadioButton("TxSource", "2", (thisTransfer.TxSource ?? 0) == 2 ? true : false, new { id = "TxSource_EdPsych" })%&gt;EdPsych &lt;br /&gt; &lt;%= Html.RadioButton("TxSource", "3", (thisTransfer.TxSource ?? 0) == 3 ? true : false, new { id = "TxSource_InpatientUnit" })%&gt;Inpatient Unit &lt;%=Html.TextBox("InpatientUnit", thisTransfer.InpatientUnit)%&gt; &lt;%= Html.ValidationMessage("InpatientUnit", "*") %&gt; &lt;%= Html.RadioButton("TxSource", "4", (thisTransfer.TxSource ?? 0) == 4 ? true : false, new { id = "TxSource_CBOC" })%&gt;CBOC &lt;%= Html.TextBox("CBOC", thisTransfer.CBOC)%&gt; &lt;%= Html.ValidationMessage("CBOC", "*") %&gt; &lt;%= Html.RadioButton("TxSource", "5", (thisTransfer.TxSource ?? 0) == 5 ? true : false, new { id = "TxSource_CLC" })%&gt;CLC &lt;%= Html.TextBox("CLC_ASIH", thisTransfer.CLC_ASIH)%&gt; &lt;%= Html.ValidationMessage("CLC_ASIH", "*") %&gt; &lt;%= Html.RadioButton("TxSource", "6", (thisTransfer.TxSource ?? 0) == 6 ? true : false, new { id = "TxSource_CNH" })%&gt;CNH &lt;%= Html.TextBox("CNH", thisTransfer.CNH)%&gt; &lt;%= Html.ValidationMessage("CNH", "*")%&gt; &lt;%= Html.ValidationMessage("TxOutSource", "*") %&gt; &lt;/p&gt; &lt;br /&gt; &lt;p&gt; &lt;label for="TxReason"&gt;Treansfer Out Reason:&lt;/label&gt;&lt;br /&gt; &lt;%= Html.RadioButton("TxReason", "1", (thisTransfer.TxReason ?? 0) == 1 ? true : false, new { id = "TxReason_EDdivert" })%&gt;EDdivert &lt;%= Html.RadioButton("TxReason", "2", (thisTransfer.TxReason ?? 0) == 2 ? true : false, new { id = "TxReason_Trauma" })%&gt;Trauma &lt;br /&gt; &lt;%= Html.RadioButton("TxReason", "3", (thisTransfer.TxReason ?? 0) == 3 ? true : false, new { id = "TxReason_OBGYN" })%&gt;OBGYN &lt;br /&gt; &lt;%= Html.RadioButton("TxReason", "4", (thisTransfer.TxReason ?? 0) == 4 ? true : false, new { id = "TxReason_BedTypeNA" })%&gt;Bed Type Not Available &lt;br /&gt; &lt;%= Html.RadioButton("TxReason", "5", (thisTransfer.TxReason ?? 0) == 5 ? true : false, new { id = "TxReason_NonV" })%&gt;Non V &lt;%= Html.RadioButton("TxReason", "6", (thisTransfer.TxReason ?? 0) == 6 ? true : false, new { id = "TxReason_NotEligible" })%&gt;Not Eligible &lt;br /&gt; &lt;%= Html.RadioButton("TxReason", "7", (thisTransfer.TxReason ?? 0) == 7 ? true : false, new { id = "TxReason_OtherServicesNotAvailable" })%&gt;Other Services Not Available &lt;%= Html.TextBox("OtherServiceNA", thisTransfer.OtherServicesNA)%&gt; &lt;%= Html.ValidationMessage("OtherServiceNA", "*") %&gt; &lt;%= Html.RadioButton("TxReason", "8", (thisTransfer.TxReason ?? 0) == 8 ? true : false, new { id = "TxReason_BackToHomeFacility" })%&gt;Transfer Back to Home Facility &lt;%= Html.TextBox("BackToHomeFacility",thisTransfer.BackToHomeFacility) %&gt; &lt;%= Html.ValidationMessage("BackToHomeFacility", "*") %&gt; &lt;%= Html.ValidationMessage("TxOutReason", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="AddedToListDate"&gt;Added to List Date:&lt;/label&gt; &lt;%= Html.TextBox("AddedToListDate", string.Format("{0:d}", thisTransfer.AddedToListDate))%&gt; &lt;%= Html.ValidationMessage("AddedToListDate", "*")%&gt; &lt;/p&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;p&gt; &lt;input type="submit" value="Save" /&gt; &lt;/p&gt; &lt;% } %&gt;&lt;!-- close using Html.BeginForm()--&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload