English 中文(简体)
有选择地打开控制者行动的新窗口
原标题:Optionally open a new window in controller Action

伙伴关系。 Net MVC 2 申请 我想做以下工作:在处理一个职位时,我要:

  • Redirect the user to other view in the current browser window
  • Open a new window showing other info (other view)

That can be done easily setting the target="_blank" attribute in the form element and adding the following jQuery script:

$(function () {
    $("form").submit(function () {
        window.location = "...";
    });
});

行动手退还的观点将在新窗口上公布。

但是,让它成为一个小trick子:

  • If there are no service layer errors when executing the action, then do the above.
  • If there s any service layer error when executing the action, then do not open the new window and the view returned by the action must be shown in the same window where the form was in the first place.

例如:Imagine说,服务层产生一种pdf文件,向用户显示,如果所有东西都ok,并且必须在新的窗口中显示pdf。

[HttpPost]
public ActionResult SomeAction(FormCollection form)
{
    var serviceMethodParams = ... // convertion from the form data somehow

    MemoryStream pdfDocument = null;

    if (!serviceLayer.DoSomething(serviceMethodParams, out pdfDocument))
    {
        // Something went wrong, do not redirect, do not open new window
        // Return the same view where error should be displayed
        return View(...);
    }

    // The service method run ok, this must be shown in a new window and the origal window must be redirected somewhere else
    return File(pdfDocument.ToArray(), "application/pdf");
}

Note that the original solution work fine when the service returns true, but if the service returns false the view showing the errors is shown in a new window and the original window is redirected somewhere else.

最佳回答

在这种情况下,更好的选择是,简单地将一份专门针对该文件的《URL》归还给用户,并在成功从你的行动方法中收回《URL》,然后打开一条提到《URL》的新窗口。 在错误方面,你可以显示错误或一些错误——基本上,数据被并入Json的回报值。

在“雅克斯”呼吁之外,我最可接受的模式是重新提出看法,然后附上一些开端 j,以打开新窗口,指出“URL”。

问题回答

暂无回答




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签