尝试从MVC中的控制器访问视图中的html元素

时间:2019-06-27 07:42:56

标签: asp.net-mvc

我正在尝试从asp.net Mvc中的Controller访问视图的html元素。 在用户名和密码文本框中填写数据后,以登录表单显示。通过Http Post发布并在控制器中访问它,现在基于控制器中的条件,我想从获取数据的位置在同一视图中操作html元素。

查看

<span id="spanInvalidCredentialsMessage" style="display:none">
  <div class="row form-group">
  <div class="col-md-8 col-md-offset-2 text-danger">
    Your credentials could not be authenticated. Please try again.
  </div>
  </div>
   <div class="row form-group">
    <div class="col-md-8 col-md-offset-2">
        <hr class="alert-danger" />
     </div>
    </div>
    </span>

视图中的登录表单字段

<div class="col-md-8 col-md-offset-2">
<span class="glyphicon glyphicon-user text-primary">
</span>Username
</div>
<div class="col-md-8 col-md-offset-2">
  @Html.TextBox("txtBxUsername", null, new { @class = "form-control" })
@Html.TextBox("txtBxPassword", null, new { @class = "form-control" })
<input id="btnLogin" type="submit" value="Login" class="btn btn-primary btn-block" />
</div>

控制器部分通过httppost获取登录详细信息

[HttpPost]
public ActionResult Login(FormCollection form)
{
//Invoke the method to authenticate the user credentials. txtBxUsername.Text.Trim().ToUpper()
string msgUserAuthenticated=objADService.GenericIsAuthenticatedWithMessage("ABC", form["txtBxUsername"].ToString().Trim().ToUpper(),form["txtBxPassword"].ToString());
  //Check if the user was authenticated.
 if (msgUserAuthenticated.Equals("Authenticated", StringComparison.InvariantCultureIgnoreCase) == true)
 {
   //Set the Session Variable and Redirect to the Home Page ASPX.
   Session[CommonConstants.SESSION_USER_ID] = form["txtBxUsername"].ToString().Trim().ToUpper();
   Session[CommonConstants.SESSION_USER_DOMAIN] = form["txtBxPassword"].ToString();
    //Redirect the user to the home page                
    //Response.Redirect("Home.aspx");
  return View("Home");            
   }
   else
    {
    //Show the error message.
   spanInvalidCredentialsMessage.Visible = true;
    //Clear the text boxes for Username and Password.
     txtBxUsername.Text = "";
     txtBxPassword.Text = "";
     return View("Login");
      }
}

期望如果错误的用户名和密码在那里,那么在控制器中我可以在视图中访问html元素ID并显示它。 实际 不知道如何实现

0 个答案:

没有答案