-1

I have a partial view which contains table and data is dynamically append to that table. When i click on save button in that form , my controller method is not firing.

Please see the below code for my partial view.

 <div id="cavitypartial">
 <table id="CTable" class=" table table-bordered">
    <tr>
        <th></th>
        <th>C Name</th>
        <th>Number of C</th>

        @foreach (var item in Model.PLByS)
            {
            <th>@item.PLName (CT/sec)</th>
            }
        <th>Comments</th>
    </tr>
    @foreach (var item in Model.CByS)
        {
            //using (Html.BeginForm("CavityUpdate", "Cavity", FormMethod.Post, new { id = "tblCavity" }))
            using (Ajax.BeginForm("CavityUpdate", "Cavity", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "cavitypartial" }))
            {


                <tr id="@item.CavityID">

                    <td><a onclick='DeleteRow(this,@item.CavityID);'><img src='/Content/Images/trash_empty.png' class='btndelete' alt='x' /></a><a onclick='EditRow(this,@item.CavityID);'><img src='~/Content/Images/edit.png' class='btndelete' alt='x' /></a></td>
                    <td><input type="text" id="cn_@item.CavityID" name="cn_@item.CavityID" value="@item.CavityName" disabled /></td>
                    <td><input type="text" id="cp_@item.CavityID" name="cp_@item.CavityID" class="addcavity" value="@item.NumberOfParts" disabled /> <input type="hidden" id="h_@item.CavityID" name="h_@item.CavityID" /> </td>
                    @foreach (var Cavitem in Model.Cavities)
                    {
                        if (item.CavityID == Cavitem.CavityID)
                        {
                            <td><input type="text" id="c_@(item.CavityID)p_@(Cavitem.ProductLineID)" name="c_@(item.CavityID)p_@(Cavitem.ProductLineID)" class="addcavity" value="@Cavitem.Time" disabled /></td>
                        }

                    }
                    <td><input type="text" id="cn_@item.CavityID" name="cc_@item.CavityID" value="@item.comments" disabled /> <input type="hidden" id="h_@item.CavityID" name="h_@item.CavityID" /> </td>
                    <td id="Savecommand"><input type="submit" id="btnS_@item.CavityID" value="Save" /></td>
                    <td id="Savecommand"><input type="button" id="btnC_@item.CavityID" onclick="cancelEditing(this,@item.CavityID);" value="Cancel" /></td>


                </tr>
            }
        }
</table>
</div>

Below is the Main page where am rendering Partial View

<div id="cavitypartial">
@Html.Partial("CavityPartialView")

</div>

I am nou using any othher form other than this.

Below is the controller code:

   [HttpPost]
    public ActionResult CavityUpdate()
    {
        // logic

        }

        return RedirectToAction("Cavity", "Cavity", new { id = siteid });


    }

Can anyone help me on this?

2 Answers2

1

Finally My controller method is firing from partial View. The mistake which i have done is, i put Html.Begin form inside div tag. I moved it to outside and place entire partail view under BeginForm.

It's working fine now.

0

Enable the submit button. Remove 'disabled' property from it.

Edit: you can't create form elements as table rows. Instead try following syntax:

<table>
        @foreach (var item in Model.CByS) { 
        <tr>
            <td>
                @using(Ajax.BeginForm("CavityUpdate", "Cavity", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "cavitypartial" })){
                    //your code
                    <table>
                        <tr>
                            <td></td>
                        </tr>
                    </table>
                }
            </td>
        </tr>
        }
    </table>
codermd
  • 720
  • 4
  • 13