Asp.net mvc 3 操作并停留在页面上
我正在构建一个ASP.NET MVC3应用程序,在其中打印一些文件。以下是我打印的代码部分:Asp.net mvc 3 操作并停留在页面上,asp.net-mvc-3,Asp.net Mvc 3,我正在构建一个ASP.NET MVC3应用程序,在其中打印一些文件。以下是我打印的代码部分: public ActionResult Barcode(string DocumentID) { barkod = new Barcode(DocumentID); MemoryStream ms = new MemoryStream(); barkod.image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); Pri
public ActionResult Barcode(string DocumentID)
{
barkod = new Barcode(DocumentID);
MemoryStream ms = new MemoryStream();
barkod.image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
Print(barkod);
return Redirect("Home/Index");
}
这可能是愚蠢的,但我怎么能只做打印而不做其他事情,不重定向或其他任何事情
我尝试了
EmptyResult
并返回null,但它给了我一个空白页。既然您似乎不想返回视图,那么为什么不通过ajax调用它呢:
$.post('@Url.Action("Barcode")', { DocumentID : docId }, function(result) {
});
您还可以包括控制器名称(例如,SomeController
):
如果打印操作不会花费很长时间,或者您只是想返回状态,无论是否花费很长时间:
public ActionResult Barcode(string DocumentID)
{
// do your thing here
return Json(the_status_a_boolean_or_some_other_type);
}
在你的js上:
$.post('@Url.Action("Barcode","Some")', { DocumentID : docId }, function(result) {
if (result) {
console.log("it's a success!");
}
else {
console.log("something wrong went bad");
}
}).error(function() {
console.log('post action cannot be completed');
});
我不想在风景方面做任何事@Action(“Barcode”,“Some”)返回一些东西,即使我用Ajax调用它,我错了吗?
Url.Action
是一个UrlHelper,它返回一个字符串。“我不想在视图方面做任何事情。”-那么,您打算如何通过回发调用条形码方法?如果是的话,那么为什么你更喜欢它而不是ajax调用,尤其是如果你不想重定向并且你想保持在同一个页面上呢?我想我不能陈述我的观点。我只想做一个操作,而不是在视图中引起任何更改,在视图中,我有一些下拉列表和项目列表根据这些下拉列表而变化。我需要一个不会返回任何内容的函数。我可以这样做吗?调用ajax帖子不会导致您的视图改变。您可以忽略返回的值,这样视图就不会更改。如果您需要进一步澄清,请邀请我在此聊天。
$.post('@Url.Action("Barcode","Some")', { DocumentID : docId }, function(result) {
if (result) {
console.log("it's a success!");
}
else {
console.log("something wrong went bad");
}
}).error(function() {
console.log('post action cannot be completed');
});