2010-01-28 398 views
1

我的應用程序是在http://www.asp.net/mvc/learn/中提供NerdDinner之後建模的。我在保存發佈的修改和創建頁面時遇到問題,即使以下幫助程序類中的業務規則都未被違反。我能想到的唯一的地方是數據庫表外鍵被違反。有人可以幫我捕捉數據庫異常嗎?謝謝。MVC無法保存數據

[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()--> 

回答

1

的數據類型,傳輸,採集參數到控制器的方法似乎是錯誤的我。我想這個數據類型應該是一個FormCollection,因爲你使用UpdateModel方法來獲取更新到你的傳輸對象。

我認爲正在發生的事情是表單正在被解析並被分割到集合對象中的Transfer類中。這個對象然後被忽略。並且因爲您的方法中沒有FormCollection對象,UpdateModel命令不會執行它應該的操作。

+0

非常感謝。我將收集參數更改爲FormCollection。 catch塊被執行。我做了另一個實驗,通過使用普通的 catch(Exception ex) {View [「Exception」] = ex.Message} 。它返回「字符串或二進制數據將被截斷,語句已被終止。」在編輯視圖頁面中,我添加了Response.Write(Request.Form)來檢查表單集合。表單中的所有數據都是正確的。我還採用了表單數據並清理了misc編碼,並使用表單數據SQL Managemant Studio構建了一條sql語句。它成功了。 – user266909 2010-01-29 08:33:06

+0

順便說一句,在這種情況下,使用FormCollection或強類型對象Transfer並不重要。他們產生相同的結果。我有另一個控制器使用強類型對象作爲參數,並正常工作。 – user266909 2010-01-29 08:47:29