c#MVC:ajax beginform在从ajax actionlink调用后无法正常工作

时间:2016-12-17 22:50:01

标签: c# ajax asp.net-mvc actionlink


我有调用ajax.actionlink的问题。 在索引视图中我有2个按钮。默认情况下,在 TestController 中调用 Editor 方法并且ajax.beginform正常工作,如果我点击超链接(ajax)来切换视图,我点击其他超链接返回上一个视图使用beginform,然后beginform不工作。为什么以及如何解决这个问题?代码如下。

索引视图

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

编辑观点

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

表格视图

@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视图

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

测试控制器

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 个答案:

没有答案