2016-05-13 97 views
0

我正在使用mvc,c#和boostrap。在我的导航栏中,我有一个下拉式登录。它被放置为像facebook登录,右上角。下拉式登录Mvc

<li class="dropdown"> 
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><img class="img-responsive" src="/images/h-perfil.png" alt="imagen"> perfil <span class="caret"></span></a> <div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;"> 
     <form method="post" action="login" accept-charset="UTF-8"> 
      <input style="margin-bottom: 15px;" type="text" placeholder="Email" id="username" name="username"> 
      <input style="margin-bottom: 15px;" type="password" placeholder="Contraseña" id="password" name="password"> 
      <input class="btn btn-primary btn-block" type="submit" id="sign-in" value="Ingresar"> 
      <label style="text-align:center;margin-top:5px">Registrarme</label> 
     </form> 
    </div> 
</li> 

我想知道如何使用模型登录用户。我也有一个使用我的模型的控制器。这是我的控制器

public ActionResult LogIn(UserModel model) 
{ 
    if (!ModelState.IsValid) //Checks if input fields have the correct format 
    { 
     return View(model); //Returns the view with the input values so that the user doesn't have to retype again 
    } 
    if (credential valids) 
    { 
     return RedirectToAction("Index", UserModel); } 

所以在这种情况下,我希望用户在点击按钮时登录。由于

+0

你能发布的usermodel类。基本上这些类的属性应该匹配输入标签的名称属性。当你提交时,UserModel类将自动填充 –

+0

嗨,谢谢你的回答。这是 –

+0

这是模型:public class userlogin { public string Email {get;组; } public string Password {get;组; } } 所以,你说它会自动填充。但是,如何在点击按钮登录时调用称为Login的控制器操作? 再次感谢! –

回答

0

分配正确的值的id和name行动attribute.Use “电子邮件” 值第一个文本框

<form method="post" action="@Url.Action("LogIn", "YourControllerName")" accept-charset="UTF-8"> 
     <input style="margin-bottom: 15px;" type="text" placeholder="Email" id="Email" name="Email"> 
     <input style="margin-bottom: 15px;" type="password" placeholder="Contraseña" id="password" name="password"> 
     <input class="btn btn-primary btn-block" type="submit" id="sign-in" value="Ingresar"> 
     <label style="text-align:center;margin-top:5px">Registrarme</label> 
</form> 

装饰与HttpPost属性:

[HttpPost] 
public ActionResult LogIn(UserModel model) 
{ 
    //code 
} 
+0

谢谢!有效! –