Ajax.Beginform only works once

I have a partial view of notes where I am trying to do inline editing, the save button only works fine once, but stops working after the first view. here is my code:

<h4>Notes</h4>
<table class="table">
    @foreach (var note in Model)
    {
        <tr>
            <td style="width:150px;">@Html.DisplayFor(modelItem => note.CreatedOn)</td>
            @if (ViewData["ShowNoteType"] == null || ((bool)ViewData["ShowNoteType"] == true)) 
            { 
                <td style="width:120px;">@Html.DisplayFor(modelItem => note.NoteType)</td>
            }

            <td class="readOnlyProperty" data-note-id=@note.NoteID>@Html.DisplayFor(modelItem => note.LoggedBy)</td>
            <td class="readOnlyProperty" data-note-id= @note.NoteID>@Html.DisplayFor(modelItem => note.Text)</td>

                @using (Ajax.BeginForm("Edit", new { id = note.NoteID }, new AjaxOptions { HttpMethod = "POST", Url = "/Notes/Edit", UpdateTargetId = "noteslist", OnComplete = "OnNotesListReloaded" }, new { @role = "form", @class = "form-inline" }))
                {
                    @Html.AntiForgeryToken()
                    <td class="editableProperty" style="display:none;" data-note-id=@note.NoteID>
                        <div class="form-group">
                            @Html.EnumDropDownListFor(modelItem => note.LoggedBy, htmlAttributes: new { @class = "form-control" })
                        </div>
                     </td>
                    <td class="editableProperty" style="display:none;" data-note-id=@note.NoteID>
                        <div class="form-group">
                            @Html.EditorFor(modelItem => note.Text, new { htmlAttributes = new { @class = "form-control" } })
                        </div>
                     </td>

                        @Html.HiddenFor(modelItem => note.NoteID)
                        @Html.HiddenFor(modelItem => note.CandidateID)
                        @Html.HiddenFor(modelItem => note.JobID)
                        @Html.HiddenFor(modelItem => note.ContactID)
                    <td class="editableProperty" style="display:none;" data-note-id=@note.NoteID>
                        <button type="submit" class="btn btn-default btn-sm save-note" title="Save">
                            <span class="glyphicon glyphicon-save" aria-hidden="true" />
                        </button>
                    </td>
                }


            <td><a class="btn btn-default edit-note" data-note-id=@note.NoteID ><span class="glyphicon glyphicon-edit" aria-hidden="true" /></a></td>
            <td class="deletebutton" data-note-id=@note.NoteID>
                @using (Ajax.BeginForm("Delete", new { id = note.NoteID }, new AjaxOptions { HttpMethod = "POST", Url = "/Notes/Delete", UpdateTargetId = "noteslist", OnComplete = "OnNotesListReloaded" }, new { @role = "form", @class = "form-inline" }))
                {
                    @Html.AntiForgeryToken()

                    <input type="hidden" name="id" value="@note.NoteID" />
                    <input type="hidden" name="CandidateID" value="@ViewData["CandidateID"]" />
                    <input type="hidden" name="JobID" value="@ViewData["JobID"]" />
                    <input type="hidden" name="ContactID" value="@ViewData["ContactID"]" />

                    <button type="submit" class="btn btn-default btn-sm" title="Delete Note">
                        <span class="glyphicon glyphicon-trash" aria-hidden="true" />
                    </button>
                }
            </td>
        </tr>
    }
</table>

    @section Scripts {

    @Scripts.Render("~/bundles/custom")
}

      

save button works fine only once. and I have to put the dropdownlist and editorfor in separate <td>

instead of putting the whole Ajax form in <td>

so they are right aligned when edited. when I put the whole Ajax.Beginform in <td>

, it works fine, but it doesn't align right. so can anyone help me make a save button and the margins are aligned correctly please.

and here is my jquery code:

var OnNotesListReloaded = function () {

    $(".edit-note").click(function (event) {

        var elem = $(this).attr("data-note-id");
        $(".edit-note[data-note-id=" + elem + "]").hide();
        $(".readOnlyProperty[data-note-id=" + elem + "]").hide();
        $(".editableProperty[data-note-id=" + elem + "]").show();
        $(".deletebutton[data-note-id=" + elem + "]").hide();

    });
}

$(document).ready(function () {

    OnNotesListReloaded();

});

      

+3


source to share


1 answer


You can use a different approach for this. Let's start with the user interface.
Step 1: Create one form only on the "For Loop" side and set the action to "/ Notes / EditDelete", don't worry, we are going to create an action in the next step.
Step 2: Now with the buttons, use an action in the name something like this, and you can also pass the ID for which the entry is being edited or deleted.

<input type="submit" value="Edit" name="action:Edit" class="btn btn-sm btn-default" />
<input type="submit" value="Delete" name="action:Delete" class="btn btn-sm btn-danger" />

      

Step 3: Now create an action filter



[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class MultipleActionsAttribute : ActionNameSelectorAttribute
    {
        public string Name { get; set; }
        public string Argument { get; set; }

        public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
        {
            var isValidName = false;
            var keyValue = string.Format("{0}:{1}", Name, Argument);
            var value = controllerContext.Controller.ValueProvider.GetValue(keyValue);

            if (value != null)
            {
                controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument;
                isValidName = true;
            }
            return isValidName;
        }
    }

      

Step 4: Use this filter over your actions:

[HttpPost]
[MultipleActions(Name = "action", Argument = "Edit")]
public ActionResult EditDelete(int id)
{
//Your edit code here
}
[HttpPost]
[MultipleActions(Name = "action", Argument = "Delete")]
public ActionResult EditDelete(int id) // you can pass the id to edit from view 
{
//Your Delete code here
}

      

0


source







All Articles