2010-01-28 379 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