c# MVC: ajax beginform not working after call from ajax actionlink

272 views Asked by At


i have problem with call ajax.actionlink. in index view i have a 2 buttons. By default is call Editor method in TestController and ajax.beginform work correctly, if i click hyperlink (ajax) to switch view and i click other hyperlink to come back to previous view with beginform, then beginform dont work. Why and how to fix that? Code is below.

Index view

<head>
    <meta name="viewport" content="width=device-width"/>
    <title> Test? </title>

    <link href="~/Content/Admin/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
</head>

<body>

    @Ajax.ActionLink( "T1", "Editor", "Test", new AjaxOptions(){ UpdateTargetId = "ii2" } )
    @Ajax.ActionLink( "T2", "Test1", "Test2", new AjaxOptions() { UpdateTargetId = "ii2" })

<div id="ii2">

</div>

</body>

Editor view

<table>
    <thead>
    <tr>
        <th> index </th>
        <th> name </th>
        <th> action </th>
    </tr>
    </thead>

    <tbody id="iTest1" >
        @Html.Action( "Table", "Test" )
    </tbody>

    <tfoot>
    <tr>
        @Html.Action( "TableAdd", "Test" )
    </tr>
    </tfoot>
</table>

Table view

@using Domain.Entity
@model IEnumerable<Domain.Entity.Language>

@foreach (Language l in Model)
{
    <tr>
        <td> @l.Index </td>
        <td> @l.Name </td>
        <td> </td>
    </tr>
}

TableAdd view

@model Domain.Entity.Language
@using (Ajax.BeginForm( "Add", "Test", new AjaxOptions() { UpdateTargetId = "iTest1" }))
{
    <th> @Html.TextBoxFor(x => x.Index) </th>
    <th> @Html.TextBoxFor(x => x.Name) </th>
    <th> <input type="submit" value="submit"/> </th>
}

Test controller

public class TestController : Controller
{
    private ILanguageRepository _repository = null;

    public TestController(ILanguageRepository repository)
    {
        _repository = repository;
    }

    public ActionResult Index()
    {
        return View( );
    }

    public ActionResult Editor()
    {
        return PartialView( "Editor" );
    }

    public ActionResult Add(Language l)
    {
        _repository.Add( l, true );
        return Table();
    }

    public ActionResult Table(IEnumerable<Language> items = null)
    {
        if (items == null)
            items = _repository.Languages;
        return PartialView( "Table", items );
    }

    public ActionResult TableAdd( Language item )
    {
        return PartialView("TableAdd", item);
    }
}
0

There are 0 answers