We help IT Professionals succeed at work.
Get Started

On postback should a TextboxFor send the data back to the action

Niall Gallagher
on
160 Views
Last Modified: 2015-02-25
I built an editable table using HTMLand jquery and it worked brilliantly then we were told that the table also needed to be sortable so a colleague took over and changed some of the code and made the table sortable but it is only now I have noticed since this was done, the table no longer posts back when you press enter after you change any thing to make the change go back to the DB. For the life of me I cannot figure out why.
I put in a bit of $.ajax to post it back to the action and it does post back now but the is no data being posted. I thought when I'm using a TextboxFor it would have happened automatically but I found out I was wrong.
here is some of the code.
part of the table
<td class="edit"; id = "AGENTID">

                        <span class="item-display">@Model.getConfirmationData[idx].AGENT_ID</span>            

                        <span class="item-field">@Html.TextBoxFor(modelItem => Model.getConfirmationData[idx].AGENT_ID, new { @class = "inputID", name="getConfirmationData[" + idx + "].AGENT_ID" })</span>

                    </td>

                    <td  class="edit", id="AGENTNAME">

                        <span class="item-display">@Model.getConfirmationData[idx].AGENT_Name</span>       

                        <span class="item-field">@Html.TextBoxFor(modelItem => Model.getConfirmationData[idx].AGENT_Name, new { @class = "inputname", name="getConfirmationData[" + idx + "].AGENT_Name"})</span> 

                    </td>        

                    <td class="edit"; id = "DBAName";>

                        <span class="item-display">@Model.getConfirmationData[idx].DBA_Name</span>       

                        <span class="item-field">@Html.TextBoxFor(modelItem => Model.getConfirmationData[idx].DBA_Name, new { @class = "inputname", name="getConfirmationData[" + idx + "].DBA_Name" })</span>

                    </td>

Open in new window


also this is the script
 

$(document).ready(
      $(document.documentElement)
        .on("click", "span.item-display", function (event)
        {
          $(this).parent().closest('tr').addClass('blue')
          var originalcontent = $(this).text();
          $(event.currentTarget)
            .hide()
            .next("span.item-field")
            .show()
            .find(":input:first")
            .focus()
            .select();
         })
        .on("keypress", "span.item-field", function (event)
        {                    
          if (event.keypress != 13 && event.which != 13)     
            return;
          event.preventDefault();   
          $.ajax({ type: "POST", url: "/AGENT/Confirmation/"})
          var $field = $(event.currentTarget),
          $display = $field.prev("span.item-display");
          $display.html($field.find(":input:first").val());
          $display.show();
          $field.hide();
        })
    );

Open in new window


and the Action
[HttpPost]

       public ActionResult Confirmation(AgentConfirmationViewModel records) //Save updated records

       {

            CWA_AgentEntities CWA = new CWA_AgentEntities();
            foreach (STAGING_TABLE_Details record in records.getConfirmationData)
            {
                STAGING_TABLE Old_Rec = CWA.STAGING_TABLE.Find(record.ID);
                Old_Rec.AGENT_ID = record.AGENT_ID;

                Old_Rec.AGENT_Name = record.AGENT_Name;

                Old_Rec.DBA_Name = record.DBA_Name;              

                [More fields.]     

            }
            CWA.SaveChanges();
            return this.Confirmation();

Open in new window


I know I am not sending data back with my ajax call but I was under the impression because I was using helpers it would take care of that + it used to work before there were changes made to the table but now I can't see what might be causing the problem.

Any help is GREATLY appreciated.
Comment
Watch Question
Developer
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE