MVC表单提交按钮将有条件地有OnClick事件

时间:2022-03-27 20:08:37

Posting a form from .cshtml. If the Email Textbox has the value of "Admin" and the Password TextBox has the value of "12345" I want to include an onclick event on the submit button.

从。cshtml发布表单。如果电子邮件文本框的值为“Admin”,密码文本框的值为“12345”,我希望在submit按钮上包含onclick事件。

I thought about if/else block but what to place in the if condition below where you see question mark (?):

我想过if/else块,但在if条件下,你看到问号(?):

<form class="fvalidate" action="/entry" method="post">
     <input type="text" name="Email" class="email required"/>
     <input type="password" name="Sifre" class="required" />
     @if (?)
     {
          <input type="submit" value="LOG IN" class="mTop15" 
          onclick="ibFunc.openBoxOpen('/entry/adv'); return false;" /> 
     }
     else
     {
         <input type="submit" value="LOG IN" class="mTop15" /> 
     }
</form>

How could I accomplish this?

我怎么能做到这一点呢?

2 个解决方案

#1


2  

You need to understand the flow of the application and what fires when. You seem to be mixing Client and Server side technology.

您需要了解应用程序的流程以及何时触发的内容。您似乎混合了客户端和服务器端技术。

The flow should be

流程应该是

  • Step 1 Submit the form
  • 第一步提交表单
  • Step 2 The controller validates the login and works out whether they are admin or not.
  • 第2步,控制器验证登录并确定它们是否为admin。
  • Step 3 The controller redirects the user to the /entry or /entry/adv page depending on whether they are an admin or not
  • 第3步:控制器将用户重定向到/条目或/条目/页面,这取决于他们是否是管理员。
  • Step 4 On the /entry page don't fire the javascript
  • 在/entry页面上的第4步不启动javascript
  • Step 4a On the /entry/adv page fire the javascript
  • 步骤4a在/入口/adv页面上启动javascript

To answer your question directly. There is nothing you can put in the place of the ? that will do what you want because that is Razor code which is executed on the server before the user has interacted with the page.

直接回答你的问题。你不能把任何东西放在那个地方?这将执行您想要的操作,因为这是在用户与页面交互之前在服务器上执行的Razor代码。

#2


0  

Do the control in POST in the controller, when the "Admin" submit the form make an if then use

在控制器中做POST控件,当“Admin”提交表单时使用if

return RedirectToAction("adv", "entry");

#1


2  

You need to understand the flow of the application and what fires when. You seem to be mixing Client and Server side technology.

您需要了解应用程序的流程以及何时触发的内容。您似乎混合了客户端和服务器端技术。

The flow should be

流程应该是

  • Step 1 Submit the form
  • 第一步提交表单
  • Step 2 The controller validates the login and works out whether they are admin or not.
  • 第2步,控制器验证登录并确定它们是否为admin。
  • Step 3 The controller redirects the user to the /entry or /entry/adv page depending on whether they are an admin or not
  • 第3步:控制器将用户重定向到/条目或/条目/页面,这取决于他们是否是管理员。
  • Step 4 On the /entry page don't fire the javascript
  • 在/entry页面上的第4步不启动javascript
  • Step 4a On the /entry/adv page fire the javascript
  • 步骤4a在/入口/adv页面上启动javascript

To answer your question directly. There is nothing you can put in the place of the ? that will do what you want because that is Razor code which is executed on the server before the user has interacted with the page.

直接回答你的问题。你不能把任何东西放在那个地方?这将执行您想要的操作,因为这是在用户与页面交互之前在服务器上执行的Razor代码。

#2


0  

Do the control in POST in the controller, when the "Admin" submit the form make an if then use

在控制器中做POST控件,当“Admin”提交表单时使用if

return RedirectToAction("adv", "entry");