使用Ajax渲染MVC视图 [英] Using Ajax to render MVC View

查看:75
本文介绍了使用Ajax渲染MVC视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要完成的任务基本上涉及我使用AJAX调用MVC视图,并将视图的内容呈现到我的主要内容div中。我理解,因为我不会进行整页重新加载,只需刷新我需要的内容,我需要更改URL字符串以考虑我试图调用的任何控制器和操作,以获得所需视图。我还没有做过很多Ajax,因为我只是一个初级开发人员,但我对学习如何实现这样的事情以及随后的任何事情非常感兴趣。

The task I am trying to accomplish basically involves me using AJAX to call an MVC view, and render the content of the view into my main content div. I understand that as I won't be doing a full page reload, just a refresh on the content I need, I will need to be altering the URL string to account for whichever controller and action I am attempting to call, to get the required view. I havent done a lot of Ajax as of yet, as I am only a junior developer, however I am very interested in learning how to achieve things such as this, and anything that follows.

我使用C#作为我的主要语言,可以使用mvc3或mvc4,无论是否有很大的不同,我不确定。

I am using C# as my primary language, and can use either mvc3 or mvc4, whether or not that makes much difference, I am not sure.

推荐答案

您可以使用Ajax.ActionLink ...

You can use Ajax.ActionLink for this...

@Ajax.ActionLink("Click here", "OtherAction", null, new AjaxOptions() { UpdateTargetId = "MyDiv" })

<div id="MyDiv">

</div>

行动

Public ActionResult OtherAction(long id)
{
    return View();
}

查看

@{
    ViewBag.Title = "Hello!";
    Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/_Layout.cshtml";
}

<h1>Hello!</h1>

这篇关于使用Ajax渲染MVC视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆