我的应用程序是在http://www.asp.net/mvc/learn/中呈现NerdDinner之后建模的。我在保存发布的编辑和创建页面时遇到问题,即使违反了以下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()-->
答案 0 :(得分:1)
我的控制器方法的集合参数的数据类型Transfer似乎是错误的。我认为这个数据类型应该是一个FormCollection,因为你使用UpdateModel方法来获取传输对象的更新。
我认为正在发生的是表单被解析并拆分为集合对象中的Transfer类。然后忽略该对象。并且因为您在方法中没有FormCollection对象,所以UpdateModel命令不会执行它应该执行的操作。