在本文中,我们将为您详细介绍ASP.NETMVC–AjaxifiedRenderAction的相关知识,此外,我们还会提供一些关于ASP.NETMVC2.0–RenderPartial和RenderA
在本文中,我们将为您详细介绍ASP.NET MVC – Ajaxified RenderAction的相关知识,此外,我们还会提供一些关于ASP.NET MVC 2.0 – RenderPartial和RenderAction之间的区别、asp.net MVC Ajax发布-redirecttoaction不起作用、ASP.NET MVC Controller FileContent ActionResult通过AJAX调用、asp.net mvc partialview @ Ajax.ActionLink不起作用的有用信息。
本文目录一览:- ASP.NET MVC – Ajaxified RenderAction
- ASP.NET MVC 2.0 – RenderPartial和RenderAction之间的区别
- asp.net MVC Ajax发布-redirecttoaction不起作用
- ASP.NET MVC Controller FileContent ActionResult通过AJAX调用
- asp.net mvc partialview @ Ajax.ActionLink不起作用
ASP.NET MVC – Ajaxified RenderAction
想象一下以下情况:我有一篇文章详细信息视图,其中文章内容下方有“添加评论”链接.点击后,评论表格会显示在帖子的内容下方.用户应该能够填充注释框,并在不刷新整个视图的情况下发送数据,只需要部分呈现的操作.此视图还必须提供在同一会话中添加的若干注释(对RenderAction()的几个AJAX调用);
哪种方法最好?
@H_301_6@解决方法
[HttpGet] public ActionResult AddComment() { return PartialView(); // presumes partial view is called "AddComment" and needs no model // you kNow what to do otherwise. }
视图:
<input type="button" value="Add Comment" onclick="addComment()" />
JavaScript的:
function addComment() { $("#comments").append("<div></div>").load("/ControllerName/AddComment"); }
这是基础知识.您可以根据需要将其变得复杂.
@H_301_6@ @H_301_6@总结
以上是小编为你收集整理的ASP.NET MVC – Ajaxified RenderAction全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
ASP.NET MVC 2.0 – RenderPartial和RenderAction之间的区别
解决方法
概要:
> RenderPartial:您负责提供模型,执行逻辑等.
> RenderAction:您负责调用操作,该控制器负责提供模型,执行逻辑等.
此外,RenderPartial将渲染一个特定的View,RenderAction可以渲染任何所需的View,这取决于控制器.例如:当您是访客(未认证)时,显示具有登录信息的视图的操作可能会返回一个视图,当您作为常规用户进行身份验证时,会显示一个视图,当您是管理员时,会显示一个视图.你,作为RenderAction的调用者根本不用关心,你只需调用你的动作
asp.net MVC Ajax发布-redirecttoaction不起作用
我在一个控制器中使用了以下代码;
if (Request.IsAjaxRequest()) { return RedirectToAction("PreviewAndSendEmail"); }
我调试了它,它涉及返回行,但是没有发生重定向。是否可以在Ajax.BeginForm内执行此操作?这是剃刀代码;
using(Ajax.BeginForm( new AjaxOptions { LoadingElementId = "loading" })) { <b>Choose E-mail Template : </b>@Html.DropDownList("emailtemps")<br /><br /> <input type="submit" value="Preview & Send" /> <span id="loading"> <img title="loading..." alt="load" src="@Url.Content("~/Content/App_Icons/gifs/loading.gif")" </span>}
答案1
小编典典您无法从服务器重定向AJAX操作。如果您希望浏览器以AJAX操作重定向,则需要使用javascript进行重定向。显然,使用AJAX重定向绝对没有用。如果您打算重定向,请使用常规的Html.Begin表单,不要打扰AJAX。
ASP.NET MVC Controller FileContent ActionResult通过AJAX调用
控制器包含一个方法public ActionResult SaveFile(),它返回一个FileContentResult.
什么工作:
该视图包含一个表单,它提交到此操作.结果是这个对话框:
什么不行:
该视图包含一些javascript来执行AJAX调用到表单将发布的相同的控制器操作.而不是触发上述对话框,甚至是AJAX成功函数,响应触发AJAX错误函数,XMLHttpRequest.responseText包含文件响应.
我需要做什么:
使用AJAX对文件进行请求,结果与提交表单时的结果相同.如何使AJAX请求提供提交表单的对话框?
解决方法
这是查看代码:
<script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function() { // hide form code here // upload to server $('#btnUpload').click(function() { $.ajax({ type: 'POST',dataType: 'json',url: '<%= Url.Action("SaveFile","Home") %>',success: function(fileId) { window.location = '<%= Url.Action("DownloadFile","Home") %>?fileId=' + fileId; },error: function() { alert('An error occurred uploading data.'); } }); }); }); </script> <% using (Html.BeginForm()) { %> <div>Field 1: <%= Html.TextBox("field1") %></div> <div>Field 2: <%= Html.TextBox("field2") %></div> <div>Field 3: <%= Html.TextBox("field3") %></div> <button id="btnUpload" type="button">Upload</button> <% } %>
这是控制器代码:
[HandleError] public class HomeController : Controller { public ActionResult Index() { return View(); } public JsonResult SaveFile(string field1,string field2,string field3) { // save the data to the database or where ever int savedFileId = 1; // return the saved file id to the browser return Json(savedFileId); } public FileContentResult DownloadFile(int fileId) { // load file content from db or file system string fileContents = "field1,field2,field3"; // convert to byte array // use a different encoding if needed var encoding = new System.Text.ASCIIEncoding(); byte[] returnContent = encoding.GetBytes(fileContents); return File(returnContent,"application/CSV","test.csv"); } public ActionResult About() { return View(); } }
asp.net mvc partialview @ Ajax.ActionLink不起作用
我的观点页面
<div id="beReplaced"> @Ajax.ActionLink("please click on me to bring the partial view","PatrialViewToBeCalled",new AjaxOptions() {UpdateTargetId = "beReplaced",InsertionMode = InsertionMode.InsertAfter,HttpMethod="Get",LoadingElementId = "prgress" }) </div>
我有一个控制器
public PartialViewResult PatrialViewToBeCalled() { var customer = db.Customers.First(); return PartialView("PartialViewThatMustBeShow",customer); }
但是当我点击生成的链接时,它会将我带到一个新页面而不是
将部分视图替换或附加到div标签.
有什么问题?
解决方法
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
在主视图中.这告诉主视图识别ajax助手.
今天的关于ASP.NET MVC – Ajaxified RenderAction的分享已经结束,谢谢您的关注,如果想了解更多关于ASP.NET MVC 2.0 – RenderPartial和RenderAction之间的区别、asp.net MVC Ajax发布-redirecttoaction不起作用、ASP.NET MVC Controller FileContent ActionResult通过AJAX调用、asp.net mvc partialview @ Ajax.ActionLink不起作用的相关知识,请在本站进行查询。
本文标签: