【问题标题】:ASP.NET MVC 3 - Partial View displayling as new pageASP.NET MVC 3 - 部分视图显示为新页面
【发布时间】:2012-12-02 00:49:24
【问题描述】:

我已经在这里看过一些帖子,但我并不聪明。我尝试了我的第一个 Ajax 示例,但无济于事。 Partial View 被加载到一个单独的页面中,而不是更改当前页面中的 dom 元素。

目的是通过单击刷新链接仅更新当前行的“上次活动日期”值。

如果有一个简单的方法,请你也告诉我吗?

查看:

@model IEnumerable<RegistrationManager.User>

@{
    ViewBag.Title = "Users";
}

@section scripts {
    @Content.Script(Url, "jquery-unobtrusive-ajax.min.js")
}
<h2>Users</h2>


<table>
    <tr>
        <th>Email Address</th>
        <th>Given Names</th>
        <th>Surname</th>
        <th>Last Activity Date</th>
        <th>Refresh</th>
    </tr>

@foreach (var item in Model)
{
    string selectedRow = "";
    if (ViewBag.UserId != null && item.UserId == ViewBag.UserId)  
    {  
        selectedRow = "selectedrow";  
    }  
    <tr class="@selectedRow" valign="top">
       <td>
            @item.UserName
        </td>
        <td>
            @item.Profile.GivenNames
        </td>
        <td>
            @item.Profile.Surname
        </td>
        <td>
            <div id="@String.Format("LastActivityDate{0}", item.UserId)">@Html.Partial("_DateOnlyPartialView", item.LastActivityDate)</div>
        </td>
        <td>
            @Ajax.ActionLink("Refresh", "Refresh",
                         new { UserId = item.UserId },
                         new AjaxOptions {
                             UpdateTargetId = "LastActivityDate" + item.UserId,
                             InsertionMode = InsertionMode.Replace,
                             HttpMethod = "GET"                                 
                        })
        </td>
    </tr>
}

</table>

控制器:

public PartialViewResult Refresh(Guid? UserId)
{
    User user = _db.Users.Find(UserId);
    user.RefreshLastActivityDate();
    return PartialView("_DateOnlyPartialView", user.LastActivityDate);
 }

【问题讨论】:

    标签: asp.net-mvc-3 asp.net-ajax


    【解决方案1】:

    您是否包含/引用了所有必要的 javascript 文件?

    如果你有 UnobtrusiveJavaScriptEnabled 那么你需要:

    1. jQuery
    2. jquery.unobtrusive-ajax.js

    如果您还使用客户端验证,则需要;

    1. jquery.validate.js
    2. jquery.validate.unobtrusive.js

    这些文件都可以在新建 MVC3 项目时找到。

    【讨论】:

    • 查看源码显示:那我错过了什么?
    • 是否有任何 javascript 错误?这些文件是否真的存在于 Scripts 文件夹中?尝试使用 javascript 文件的非最低版本。我在 MVC 3 项目中看到了它们的损坏版本
    • 没有 JavaScript 错误。是的,它们存在。我没有更改标准 ASP.NET MVC3 项目的任何内容。我已经添加了你提到的脚本。虽然不确定第一个提到的。 @section 脚本 { @Content.Script(Url, "jquery-unobtrusive-ajax.min.js") @Content.Script(Url, "jquery.validate.js") @Content.Script(Url, "jquery.validate. unobtrusive.js") }
    • 问题解决了第一个文件的拼写错误。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 2012-01-03
    • 2012-05-15
    • 1970-01-01
    • 2016-03-09
    相关资源
    最近更新 更多