views:

259

answers:

3

I have a controller with the following two Edit methods. The edit form displays correctly with all additional dropdown lists from the 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. 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));
        }
    }
}
A: 

For default model binding to work, your form variable names should match your model's property names. That may solve your problem.

darthjit
I posted the Edit.aspx and ASCX. As you can see the field names in the form correspond to the data model's property names. When I view the postback collection, the fields contains none of the ones I posted back.
yes but you are binding a veteran object on page and your code is expecting a transfer object! Try using a veteran object on Edit method instead of the transfer object. public ActionResult Edit(int id, Veteran collection)
darthjit
A: 

Here is the Edit.aspx and the user control ASCX:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

Edit

<% Html.RenderPartial("TransferForm"); %>

<div>
    <%=Html.ActionLink("Back to List", "Index") %>
</div>

===================================================================================

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <% 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()-->
A: 

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? 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.

<%@ 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

<%= 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".

You could also leave the names without the "Veteran" prefix and instead bind to a Veteran object directly.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id,[Bind(Prefix="")]Veteran veteran)
{

Hope this helps.

madcapnmckay