[英]asp.net mvc postback
I have a controller with the following two Edit methods. 我有一个具有以下两个Edit方法的控制器。 The edit form displays correctly with all additional dropdown lists from the FormViewModel.
编辑表单正确显示,并带有FormViewModel中的所有其他下拉列表。 However, when I changed some field values and submitted the form.
但是,当我更改一些字段值并提交表单时。 None of the changed fields were saved.
未保存任何更改的字段。 The fields in the postbask collection have default or null values.
postbask集合中的字段具有默认值或空值。 I have another edit form which update another table.
我有另一个编辑表单,用于更新另一个表。 On submit, the changed values are saved.
提交后,更改的值将保存。 Does anyone know why?
有人知道为什么吗?
// GET: /Transfers/Edit/5
public ActionResult Edit(int id)
{
Transfer transfer = myRepository.GetTransfer(id);
if (transfer == null)
return View("NotFound");
return View(new TransferFormViewModel(transfer));
}
//
// POST: /Transfers/Edit/5
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, Transfer collection)
{
Transfer transfer = vetsRepository.GetTransfer(id);
if (transfer == null)
return View("NotFound");
else
{
try
{
UpdateModel(transfer);
vetsRepository.Save();
return RedirectToAction("Details", new { id = transfer.TransfersID });
}
catch
{
ModelState.AddModelErrors(transfer.GetRuleViolations());
return View(new TransferFormViewModel(transfer));
}
}
}
Here is the Edit.aspx and the user control ASCX: 这是Edit.aspx和用户控件ASCX:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<VETS.Controllers.TransferFormViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.RenderPartial("TransferForm"); %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
=================================================================================== ================================================== ================================
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<VETS.Controllers.TransferFormViewModel>" %>
<%
var thisTransfer = Model.Transfer;
var thisVeteran = thisTransfer.Veteran;
%>
<%= Html.ValidationSummary("Please correct the errors and try again.") %>
<% using (Html.BeginForm()) { %>
<fieldset>
<legend><%= (thisTransfer == null) ? string.Empty : (thisTransfer.OffList ? "Off List" : " On List")%></legend>
<div class="sideBySideDiv">
<p>
<label for="LastName">Last Name:</label>
<%= thisVeteran.LastName %>
</p>
<p>
<label for="FirstName">First Name:</label>
<%= thisVeteran.FirstName %>
</p>
<p>
<label for="LastFourSSN">SSN:</label>
<%= thisVeteran.LastFourSSN%>
</p>
<p>
<label for="DOB">DOB:</label>
<%= String.Format("{0:d}", thisVeteran.DOB)%>
</p>
<p>
<label for="Hospital">Hospital:</label><br />
<%= Html.DropDownList("Hospital", Model.NonVaHospitals)%>
</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="_SC">%SC:</label>
<%= thisVeteran.PercentSC._SC%>
</p>
<p>
<label for="PCP">PCP:</label>
<%= thisVeteran.PCP %>
</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.Caller_Name)%>
</p>
<p>
<label for="CallerPhone">Caller Number:</label>
<%= Html.TextBox("CallerPhone", thisTransfer.CallerNumber)%>
</p>
<p><label for="TransportElig">Eligibility:</label>
<%= thisVeteran.TransportElig ? "Yes" : "No" %>
</p>
</div>
<div class="sideBySideDiv">
<p>
<label for="CaseMgr">Case Mgr:</label>
<%= Html.TextBox("CaseMgr", thisTransfer.CaseMgrName)%>
</p>
<p>
<label for="MgrPhone">Mgr Phone:</label>
<%= Html.TextBox("MgrPhone", thisTransfer.CaseMgrPhone)%>
</p>
<p>
<label for="CPRSDate">CPRS Date:</label>
<%= Html.TextBox("CPRSDate", string.Format("{0:d}",thisTransfer.CPRSDate))%>
<%= Html.ValidationMessage("CPRSDate", "*") %>
</p>
<p>
<label for="CPRSTime">CPRS Time:</label>
<%= Html.TextBox("CPRSTime", string.Format("{0:t}",thisTransfer.CPRSTime))%>
<%= Html.ValidationMessage("CPRSTime", "*") %>
</p>
<p>
<label for="TxPriority">TXFR Priority:</label>
<%= Html.TextBox("TxPriority", thisTransfer.TXFRPriority)%>
<%= Html.ValidationMessage("TxPriority", "*") %>
</p>
<p>
<label for="AcceptBy">Accepted By:</label>
<%= Html.TextBox("PreferMD", 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="InfectionType">Infection Type:</label>
<%= Html.DropDownList("InfectionType", Model.InfectionTypes)%>
<%= Html.ValidationMessage("InfectionType", "*") %>
</p>
<p>
<label for="COJ">COJ:</label>
<% if (thisVeteran.VAHospital != null) %>
<%= thisVeteran.VAHospital.VAHospital1 %>
</p>
<p>
<label for="FeePay">Fee Pay:</label>
<%= Html.DropDownList("FeePay", Model.FeePayReasons) %>
<%= Html.ValidationMessage("FeePay", "*") %>
</p>
<p>
<label for="Insurance">Insurance:</label>
<%= thisVeteran.Insurance %>
</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 />
<input type="radio" name="TxSource" id="radEd" title="ED"/>Ed
<input type="radio" name="TxSource" id="radEdPsych" title="EdPsych"/>EdPsych
<%= Html.ValidationMessage("TxOutSource", "*") %>
</p>
<p>
<label for="InpatientUnit">Inpatient Unit:</label>
<%=Html.TextBox("InpatientUnit", thisTransfer.InpatientUnit)%>
<%= Html.ValidationMessage("InpatientUnit", "*") %>
</p>
<p>
<label for="CBOC">CBOC:</label>
<%= Html.TextBox("CBOC", thisTransfer.CBOC)%>
<%= Html.ValidationMessage("CBOC", "*") %>
</p>
<p>
<label for="CLC">CLC (ASIH):</label>
<%= Html.TextBox("CLC_ASIH", thisTransfer.CLC_ASIH)%>
<%= Html.ValidationMessage("CLC_ASIH", "*") %>
</p>
<p>
<label for="TxReason">Treansfer Out Reason:</label><br />
<input type="radio" id="radEDdivert" name="txReason" title="EDdivert" />Ed Divertc<br />
<input type="radio" id="radTrauma" name="txReason" title="Trauma" />Trauma<br />
<input type="radio" id="radOBGYN" name="txReason" title="OBGYN" />OBGYN<br />
<input type="radio" id="radBedNA" name="txReason" title="Bed Type Not Available" />Bed Type Not Available<br />
<input type="radio" id="radNoVet" name="txReason" title="Non_Veteran" />Non Veteran<br />
<input type="radio" id="radNotEligible" name="txReason" title="Not_Eligible" />Not_Eligible<br />
<input type="radio" id="radOtherServiceNA" name="txReason" title="Other Services Not Available" />Other Services Not Available<br />
<input type="radio" id="radTxBackHome" name="txReason"title="Transfer Back to Home VA" />Transfer Back to Home VA<br />
<%= Html.ValidationMessage("TxOutReason", "*") %>
</p>
<p>
<label for="OtherServiceNA">Service Not Available:</label>
<%= Html.TextBox("OtherServiceNA", thisTransfer.OtherServicesNA)%>
<%= Html.ValidationMessage("OtherServiceNA", "*") %>
</p>
<p>
<label for="BackToHomeVA">TXFR to Home VA:</label>
<%= Html.TextBox("BackToHomeVA",thisTransfer.BackToHomeVA) %>
<%= Html.ValidationMessage("BackToHomeVA", "*") %>
</p>
<p>
<label for="TxID">Transfer Record ID:</label>
<% %>
<%= Html.ValidationMessage("TxID", "*") %>
</p>
<p>
<label for="ListDate">Added to List Date:</label>
<%= Html.TextBox("ListDate", string.Format("{0:d}", thisTransfer.AddedToListDate))%>
<%= Html.ValidationMessage("ListDate", "*") %>
</p>
</div>
</fieldset>
<p>
<input type="submit" value="Save" />
</p>
<% } %><!-- close using Html.BeginForm()-->
You are not binding the values back to the model when you do 在执行操作时,您没有将值绑定回模型
@Html.DropDownList("Hospital", Model.NonVaHospitals)
@Html.TextBox("TxPriority", thisTransfer.TXFRPriority)
@Html.TextArea("StatusReport", thisTransfer.StatusReport)
Instead you should be using this 相反,您应该使用这个
@Html.DropDownListFor(model=>model.SelectedNonVaHospitals, Model.NonVaHospitals)
@Html.TextBoxFor(model=>model.TXFRPriority)
@Html.TextAreaFor(model=> model.StatusReport)
For default model binding to work, your form variable names should match your model's property names. 为了使默认模型绑定起作用,您的表单变量名称应与模型的属性名称匹配。 That may solve your problem.
那可以解决您的问题。
It looks to me like you have a Transfer object which has a Veteran property on it and this is what you are trying to edit? 在我看来,您好像有一个具有老兵属性的Transfer对象,这就是您要编辑的对象? Correct me if I got this all wrong..
如果我弄错了,请纠正我。
Firstly I would strongly type your views and controls. 首先,我强烈建议您输入视图和控件。
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<Transfer>" %>
Im assuming the type of Veteran is Veteran, it may be person or something else I don't know. 我假设退伍军人的类型是退伍军人,这可能是人或我不知道的其他事情。 Once you have this you can remove the var Model.Transfer etc.
一旦有了这个,您可以删除var Model.Transfer等。
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Veteran>" %> <% var thisTransfer = Model.Transfer; var thisVeteran = thisTransfer.Veteran; %>
Then I would alter your edit page to be 然后,我将您的编辑页面更改为
<% Html.RenderPartial("TransferForm", Model.Veteran); %>
As you have bound to a Transfer object but the form is returning data for a Veteran object your names in the user control would need to be named 由于已绑定到Transfer对象,但是表单正在返回Veteran对象的数据,因此需要将用户控件中的名称命名
<%= Html.TextBox("collection.Veteran.AdmitDX", Model.AdmitDX)%>
In order to bind correctly I believe you can use the Bind(Prefix="")
attribute to override the binder prefix which by default looks at the name of the property on your action method, in this case "collection". 为了正确绑定,我相信您可以使用
Bind(Prefix="")
属性覆盖活页夹前缀,该活页夹前缀默认情况下会查看您的操作方法上的属性名称,在本例中为“ collection”。
You could also leave the names without the "Veteran" prefix and instead bind to a Veteran object directly. 您也可以保留名称而不使用“ Veteran”前缀,而是直接绑定到Veteran对象。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id,[Bind(Prefix="")]Veteran veteran)
{
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.