.Net MVC 视图不渲染

发布于 2024-12-10 13:53:23 字数 1788 浏览 0 评论 0原文

我有一个下面列出的控制器:

//
// GET: /Customer/Details/5
public ActionResult Details(short id)
{
    ActionResult actionResult = null;

    if (HttpContext.User.IsInRole("Admin"))
    {
        // this is the logic that is getting executed
        YeagerTechWcfService.Customer cust = db.GetCustomerID(Convert.ToInt16(id));
        actionResult = View("Details", cust);           }
    else
    {
        HttpCookie cn = Request.Cookies["strCookieName"];
        if (cn != null)
        {
            YeagerTechWcfService.Customer cust = db.GetCustomerID(Convert.ToInt16(id));
            actionResult = View("Details", cust);
        }
        else
        {
            TempData["ErrCode"] = "CustView";
            actionResult = RedirectToAction("Index", "Home");
        }
    }

    return actionResult;
}

我有一个如下所示的视图(ActionLink 所在的位置):

columns.Template(
    @<text>
    @Ajax.ActionLink("Detail", "Details", "Customer", new { id = item.CustomerID },
    new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "detailCustomer" })
    </text>
).Width(70); 

现在呈现的输出如下:

<a data-ajax="true" data-ajax-mode="replace" data-ajax-update="#detailCustomer" href="/Customer/Details/2">Detail</a> 

如果我在浏览器的源视图中单击链接,我很好地到达了我的新视图。

但是,如果我尝试单击 ActionLink,则视图不会出现。我可以在调试期间验证,在点击该控制器代码后我正在单步执行详细信息视图。当前视图只是保留在原处,而不切换到新视图。

此外,我可以看到,如果我单击 ActionLink,它会执行相同的代码(在调试期间)与将其粘贴到地址栏时相同:

http://localhost:4514/Customer/Details/2

当我单击 ActionLink 时,即使执行相同的代码地址 url 不会更改为上面的。并且视图没有被呈现。

我做错了什么?

I have a Controller listed below:

//
// GET: /Customer/Details/5
public ActionResult Details(short id)
{
    ActionResult actionResult = null;

    if (HttpContext.User.IsInRole("Admin"))
    {
        // this is the logic that is getting executed
        YeagerTechWcfService.Customer cust = db.GetCustomerID(Convert.ToInt16(id));
        actionResult = View("Details", cust);           }
    else
    {
        HttpCookie cn = Request.Cookies["strCookieName"];
        if (cn != null)
        {
            YeagerTechWcfService.Customer cust = db.GetCustomerID(Convert.ToInt16(id));
            actionResult = View("Details", cust);
        }
        else
        {
            TempData["ErrCode"] = "CustView";
            actionResult = RedirectToAction("Index", "Home");
        }
    }

    return actionResult;
}

I have a View (where the ActionLink is) like below:

columns.Template(
    @<text>
    @Ajax.ActionLink("Detail", "Details", "Customer", new { id = item.CustomerID },
    new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "detailCustomer" })
    </text>
).Width(70); 

The rendered output is now as follows:

<a data-ajax="true" data-ajax-mode="replace" data-ajax-update="#detailCustomer" href="/Customer/Details/2">Detail</a> 

If I click on the link from within the source view of the browser, I get to my new View just fine.

However, if I try and click on the ActionLink, the View doesn't come up. I can verify during debugging, that I'm stepping thru the Details View after I hit that controller code. The present view just stays in place without switching to the new View.

Moreover, I can see that if I click on the ActionLink, it executes the same exact code (during debugging) as when I paste it into the address bar:

http://localhost:4514/Customer/Details/2

When I click on the ActionLink, even though the same code is executed, the address url doesn't change to the above. and the View is not being rendered.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是在用心讲痛 2024-12-17 13:53:23

即使执行相同的代码,地址url也不会改变
到上面

您使用的是 Ajax.ActionLink ,这意味着它将发送 AJAX 请求。 AJAX 的重点是保持在同一页面上而不是重定向。

您指定 UpdateTargetId = "detailCustomer",因此请确保您的页面中有一个具有此 id 的容器:

<div id="detailCustomer"></div>

它将使用 AJAX 调用的结果进行更新。还要确保您已将 jquery.unobtrusive-ajax.js 脚本正确包含到您的页面中,以便 Ajax.ActionLink 帮助程序执行任何有用的操作。

另一方面,如果您想执行完整的回发并更改浏览器的 url,您可能需要一个标准链接:

Html.ActionLink("Detail", "Details", "Customer", new { id = item.CustomerID }, null)

even though the same code is executed, the address url doesn't change
to the above

You are using an Ajax.ActionLink meaning that it will send an AJAX request. The whole point of AJAX is to stay on the same page and not redirect.

You indicate UpdateTargetId = "detailCustomer" so make sure that in your page you have a container with this id:

<div id="detailCustomer"></div>

which will be updated with the results of the AJAX call. Also make sure that you have properly included the jquery.unobtrusive-ajax.js script to your page in order for the Ajax.ActionLink helper to do anything useful.

On the other hand if you want to perform a full postback and change the url of your browser you probably need a standard link:

Html.ActionLink("Detail", "Details", "Customer", new { id = item.CustomerID }, null)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文