使用重定向方法将数据从控制器传递到视图

发布于 2025-01-12 07:42:10 字数 2249 浏览 2 评论 0原文

我有一个 C# 语言的 ASP.Net Web 应用程序,我想在我的 report.cshtml 视图中显示一些可视化图表。
目前,我正在使用 report.cshtml 视图的 httpget 函数将数据传递到我的视图。但用这种方法,我无法过滤数据。这就是我想做的:

当用户连接到我的网站时,它会在我的 HomeController 中启动我的 Httppost 函数,并使用用户名和地址。密码作为参数。
我使用此函数通过response.redirect方法重定向到我的report.cshtml视图,其中用户将其个人数据作为可视化。

我的数据库本地存储在名为 my_list 的变量中,我可以在 httppost 函数中使用该变量。

我想要的是在发生重定向时将此列表粘贴到 report.cshtml 的 js 部分。 使用重定向方法可以实现这一点吗?
或者,如果不行,还有其他解决方案吗?

我的 Index.cshtml :


<form method="post" , enctype="multipart/form-data">

            <input required id="my_pseudo" name="my_pseudo" v-model="pseudo" placeholder="Username" />
            <input required id="my_password" name="my_password" type="password" v-model="password" placeholder="password" />

            <Button id="test_click" Text="Connexion">Connection</Button>

        </form>

我的 httpget 函数 :

public ActionResult EmbedReport()
        {

            List<Dashboard___Veins_Data___By_Institution> my_list = fct_BO.Class1.My_function("my_user");
            // It's at this moment that the filter is applied, so I want that, instead of "my_user", I can put a variable which is the "Contact_name" of the user currently connected.
            ViewBag.Message = my_list;


            return View();
        }

我的 httppost 函数

public ActionResult Index(string my_pseudo, string my_password)
        {
            
            
            
            //It's not good for the moment, but it's here that I want to see if "my_pseudo" and "my_password" match a line of my table "User_connection". If yes, the redirect method occurs and I would like to past my list at the same time ->

             if (...)
                  Response.Redirect("~/Home/Report");
                  

            // If not, then the user will receive an error message.
             else
                Response.Write("<script>alert('Login or password incorrect')</script>");

            return View();
            
        }

我没有完成这个函数,因为我的我的数据库中的表“User_connection”目前尚未完成。

另外,有人建议我使用 RedirectResult 而不是 response.redirect。这是更好的解决方案吗?

谢谢。

I have an ASP.Net web application in C# in which I want to show some visualizations graphs in my report.cshtml View.
For the moment, I'm using the httpget function of my report.cshtml View to past the data to my View. But with this method, I can't filter the data. So this is what I want to do :

When an user connect in my website, it launch my Httppost function inside my HomeController, with the username & password as arguments.
I use this function to redirect to my report.cshtml View with the response.redirect method, in which the user will should his personnal data as visualizations.

My database is stored locally in a variable named my_list which I can use in my httppost function.

What I want is to past this list to the js section of my report.cshtml when the redirection occurs.
Is this possible by using the redirect method?
Or, if not, is there another solution to do it ?

My Index.cshtml :


<form method="post" , enctype="multipart/form-data">

            <input required id="my_pseudo" name="my_pseudo" v-model="pseudo" placeholder="Username" />
            <input required id="my_password" name="my_password" type="password" v-model="password" placeholder="password" />

            <Button id="test_click" Text="Connexion">Connection</Button>

        </form>

My httpget function :

public ActionResult EmbedReport()
        {

            List<Dashboard___Veins_Data___By_Institution> my_list = fct_BO.Class1.My_function("my_user");
            // It's at this moment that the filter is applied, so I want that, instead of "my_user", I can put a variable which is the "Contact_name" of the user currently connected.
            ViewBag.Message = my_list;


            return View();
        }

My httppost function

public ActionResult Index(string my_pseudo, string my_password)
        {
            
            
            
            //It's not good for the moment, but it's here that I want to see if "my_pseudo" and "my_password" match a line of my table "User_connection". If yes, the redirect method occurs and I would like to past my list at the same time ->

             if (...)
                  Response.Redirect("~/Home/Report");
                  

            // If not, then the user will receive an error message.
             else
                Response.Write("<script>alert('Login or password incorrect')</script>");

            return View();
            
        }

I didn't finish this function, because my table "User_connection", in my database, is not finished for the moment.

Also, somebody adviced me to use RedirectResult instead of response.redirect. Is it a better solution?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好菇凉咱不稀罕他 2025-01-19 07:42:10

我可以建议你将你的行为包装在接口中吗?

这样你就有了一个接口

public interface IErrorHandler {
   void provideErrorResponse(HttpResponse response);
}

public interface ISuccessHandler {
   void provideSuccessfulResponse(HttpResponse response);
}

因为现在你可以实现小型的单一用途类,以某种方式处理正确或不正确的响应,这意味着如果你需要以相同的方式错误处理多个结果(非常有可能),你可以简单地依赖注入将错误处理程序也添加到其他控制器中,并且只有一组接口测试,而不是一组测试来测试每个控制器上的相同行为。

就您的最后一个问题而言:

Response.Redirect("http://www.microsoft.com/gohere/look.htm");

调用 Redirect 相当于在第二个参数设置为 true 的情况下调用 Redirect。Redirect

调用 End,它会在完成时抛出 ThreadAbortException 异常。此异常对 Web 应用程序性能有不利影响。因此,我们建议您可以使用 HttpResponse.Redirect(String, Boolean) 重载来代替此重载,并为 endResponse 参数传递 false,然后调用 CompleteRequest 方法。有关详细信息,请参阅 End 方法。

摘自:
https://learn. microsoft.com/en-us/dotnet/api/system.web.httpresponse.redirect?view=netframework-4.8

这可能就是他建议您不要使用它的原因。但这完全是猜测,因为我不知道当时“某人”指的是什么:)

举个例子:

public class HomeController {
   private IErrorHandler _errorHandler;
   private ISuccessHandler _successHandler; 

   public class HomeController (IErrorHandler errorHandler, ISuccessHandler successHandler)
   {
      _errorHandler = errorHandler;
      _successHandler = successHandler;
   }

   public ActionResult Index(string my_pseudom, string my_password)
   {
      if (...)
         _successHandler.provideSuccessfulResponse(Response);
                  
      // If not, then the user will receive an error message.
      else
         _errorHandler.provideErrorResponse(Response));
   }

}

请注意,您甚至可以重复使用成功的响应,前提是它们在成功响应的方式上没有差异 出于“过滤”视图的目的而进行处理(不太可能)

,这通常是通过端点到后端来完成的,方法是将一组参数从视图传递到控制器,然后传递到后端,并且然后生成的项目列表将被过滤在后端并作为响应返回。

因此,当您说您希望通过重定向来完成此操作时,我不确定我是否完全遵循您的意图?

像这样的东西吗?

public interface ILoginHandler {
   ILoginResult HandleLogin(string username, string password); 
}

public inteface ILoginResult {
   bool WasValid {get;set;}
}

public class HomeController {
       private IErrorHandler _errorHandler;
       private ISuccessHandler _successHandler; 
       private ILoginHandler _loginHandler;
    
       public class HomeController(IErrorHandler errorHandler, ISuccessHandler successHandler, ILoginHandler loginHandler)
       {
          _errorHandler = errorHandler;
          _successHandler = successHandler;
          _loginHandler = loginHandler;
       }
    
       public ActionResult Index(string my_pseudom, string my_password)
       {    
          ILoginResult loginResult = _loginHandler.HandleLogin(my_pseudom, my_password);
          if (loginResult.WasValid)
             _successHandler.provideSuccessfulResponse(Response);
          else
             _errorHandler.provideErrorResponse(Response));
       }
    
    }

根据谁登录,您可以创建一个工厂模式,它接受用户名,然后向后端提供特定请求并返回您想要的列表,如果您想要过滤,请提供过滤值作为 URL 的一部分,或者将其设为帖子,这样您就可以通过这种方式提供参数。

然后更新您的工厂以也使用这些参数。那么根本就不应该成为问题。

May I suggest that you wrap your behaviour in interfaces?

so that you have an Interface

public interface IErrorHandler {
   void provideErrorResponse(HttpResponse response);
}

and

public interface ISuccessHandler {
   void provideSuccessfulResponse(HttpResponse response);
}

Because now you can implement small single purpose classes that process a correct or incorrect response a certain way, meaning if you need to error handle more than one result the same way (VERY LIKELY) you can simply dependency inject the error handler into your other controllers as well, and only have 1 set of tests for the interface, instead of a suite of tests to test the same behaviour on each controller.

In terms of your final question:

"

Response.Redirect("http://www.microsoft.com/gohere/look.htm");

Calling Redirect is equivalent to calling Redirect with the second parameter set to true.

Redirect calls End which throws a ThreadAbortException exception upon completion. This exception has a detrimental effect on Web application performance. Therefore, we recommend that instead of this overload you use the HttpResponse.Redirect(String, Boolean) overload and pass false for the endResponse parameter, and then call the CompleteRequest method. For more information, see the End method."

Taken from:
https://learn.microsoft.com/en-us/dotnet/api/system.web.httpresponse.redirect?view=netframework-4.8

That is likely why he recommended you not use it. but this is a total guess, as I cannot know what "somebody" was referencing at the time :)

As an example:

public class HomeController {
   private IErrorHandler _errorHandler;
   private ISuccessHandler _successHandler; 

   public class HomeController (IErrorHandler errorHandler, ISuccessHandler successHandler)
   {
      _errorHandler = errorHandler;
      _successHandler = successHandler;
   }

   public ActionResult Index(string my_pseudom, string my_password)
   {
      if (...)
         _successHandler.provideSuccessfulResponse(Response);
                  
      // If not, then the user will receive an error message.
      else
         _errorHandler.provideErrorResponse(Response));
   }

}

Notice that you can even re-use successful responses provided they don't have a difference in how a success response should be handled (Less likely)

for purpose of "filtering" your view, that is usually done via the end-point to back-end by passing a set of parameters from your view, to the controller, then to the back-end, and then the resulting list of items, will be filtered at the back-end and provided back as a response.

So when you say you wish to accomplish this via re-direct I am not sure I completely follow what you intended to do?

Something like this?

public interface ILoginHandler {
   ILoginResult HandleLogin(string username, string password); 
}

public inteface ILoginResult {
   bool WasValid {get;set;}
}

public class HomeController {
       private IErrorHandler _errorHandler;
       private ISuccessHandler _successHandler; 
       private ILoginHandler _loginHandler;
    
       public class HomeController(IErrorHandler errorHandler, ISuccessHandler successHandler, ILoginHandler loginHandler)
       {
          _errorHandler = errorHandler;
          _successHandler = successHandler;
          _loginHandler = loginHandler;
       }
    
       public ActionResult Index(string my_pseudom, string my_password)
       {    
          ILoginResult loginResult = _loginHandler.HandleLogin(my_pseudom, my_password);
          if (loginResult.WasValid)
             _successHandler.provideSuccessfulResponse(Response);
          else
             _errorHandler.provideErrorResponse(Response));
       }
    
    }

based on who logs in, you could make a factory pattern, that takes the username, and then provides a specific request to your back-end and returns the list you want, if you want filtering, provide the filtering values as part of the URL, or make it a post, so you can provide parameters that way.

And then update your factory to use these parameters also. Shouldn't be a problem then at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文