简体   繁体   English

ASP.NET C# 响应。写入不显示消息

[英]ASP.NET C# Response.Write doesn't display message

So I am told just by using Javascript inside the Response.Write method, I can display an alert/message box on ASP.NET C#.所以我被告知只要在 Response.Write 方法中使用 Javascript,我就可以在 ASP.NET C# 上显示一个警报/消息框。

Using Response.Write("<script>alert('Data inserted successfully')</script>") as the template given in other answers.使用Response.Write("<script>alert('Data inserted successfully')</script>")作为其他答案中给出的模板。

When I first attempted to use it, it worked, displaying the message and redirecting me sucessfully.当我第一次尝试使用它时,它可以正常工作,显示消息并成功重定向我。 Since then, it has not worked, I only changed the message to display and also where the redirect URL would go after.从那以后,它就没有用了,我只更改了要显示的消息,以及重定向 URL 之后将 go 的位置。 Logically it is working as it is taking me back to the application form, it just isn't displaying the message.从逻辑上讲,它正在工作,因为它将我带回申请表,它只是没有显示消息。

Can anyone explain why it suddenly doesn't work?谁能解释为什么它突然不起作用?

Code;代码;

        public ActionResult UpdateApplication(int ApplicationId, ApplicationEdit applicationEdit)
    {
        if(applicationEdit.Firm == false)
        try
        {
            applicationService.UpdateApplication(applicationEdit, ApplicationId);
            return RedirectToAction("GetUser", "User", new { ApplicationId = applicationEdit.ApplicationId });
        }
        catch
        {
            return View();
        }
        else
        {
            Response.Write("<script language=javascript>alert('Sorry, You can only have 1 firm application to send off, please update the old application and untick firm option on other application first.')</script>");
            return RedirectToAction("UpdateApplication", new { ApplicationId = applicationEdit.ApplicationId });
        }
    }

Because you're redirecting the user:因为您正在重定向用户:

return RedirectToAction("UpdateApplication", new { ApplicationId = applicationEdit.ApplicationId });

This returns a redirect HTTP code to the browser (HTTP 307 or 308 I imagine) which tells the browser to direct the user to the specified URL.这会将重定向 HTTP 代码返回到浏览器(我想是 HTTP 307 或 308),它告诉浏览器将用户定向到指定的 URL。 Since the browser knows it's redirecting the user, it has no reason to render any content in the response.由于浏览器知道它正在重定向用户,它没有理由在响应中呈现任何内容。

Either return content to be rendered in the browser or return a redirect response, but not both.返回要在浏览器中呈现的内容返回重定向响应,但不能同时返回两者。 If you return the view instead then you should see your <script> element in the resulting response.如果您返回视图,那么您应该在结果响应中看到您的<script>元素。 If you want to redirect then any message you want to display to the user should be on that next page, not in the current response.如果您想重定向,那么您想向用户显示的任何消息都应该在下一页上,而不是在当前响应中。


As an aside, using Response.Write in an ASP.NET MVC application is pretty much always the wrong approach.顺便说一句,在 ASP.NET MVC 应用程序中使用Response.Write几乎总是错误的方法。 Any client-side code should be in or referenced by the view, and you can use data on the view's model to conditionally render or not render that content.任何客户端代码都应该在视图中或被视图引用,并且您可以使用视图的 model 上的数据来有条件地呈现或不呈现该内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM