2017-07-24 19 views
0

我有一个用户可以用来登录的登录页面,我在控制器中将其方法定义为HttpPost,当我尝试从浏览器访问它时,它显示没有找到文件,如果我删除HttpPost属性,它击中控制器并返回视图。它将形式值传递给控制器​​,如果我没有提及它的类型为HttpPost。这里是我的Login/SignIn.cshtml代码:无法调用方法,如果我提到它的属性为HttpPost

@model BOL.Player_Access 
@using (Html.BeginForm("SignIn","Login",FormMethod.Post)){ 
@Html.AntiForgeryToken() 
<div class="form-horizontal"> 
    <h4>Sign In</h4> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.LabelFor(model => model.PlayerEmail, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.PlayerEmail, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.PlayerEmail, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="SignIn" class="btn btn-default" /> 
     </div> 
    </div> 
</div> 

而我的LoginController代码是在这里

[AllowAnonymous] //This filter removes authorize filter for this controller alone and allow anonyomous request 
public class LoginController : Controller 
{ 
    // GET: Login 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult SignIn(Player_Access plyr_obj) 
    { 
     return View(); 
    } 
} 
+0

发布您的完整代码。缺少结束大括号。 –

回答

3

您需要GET和POST代码。

你需要一个GET行动表明将POST'ed

public ActionResult SignIn() 
{ 
    return View(); 
} 

这将显示签到视图的形式。

然后你需要一个POST动作从表单中取值并发送给控制器。

[HttpPost] 
public ActionResult SignIn(Player_Access plyr_obj) 
{ 
    //do some work to authenticate the user 

    return View(); 
} 
+0

,它的工作原理..谢谢。但是这是完成这个兄弟的正确方法,我不能在一个函数中实现它吗?我在视频教程中看到,他们使用单一操作方法发布数据和显示表单在一个action.Any想法他们如何实现它bro,MVC支持这个功能在任何旧版本? – ArunKumar

+0

我不知道任何版本的MVC“支持”单个显示/后置语义。在可能有效的最微不足道的情况下,但是一旦您开始在网页上显示未回传的属性(如选择列表),就会崩溃。 – Fran

+0

也调查PRG模式。一旦你发布你应该重新导向到一个创建获取或索引,以便你不能无意中连续多次发布。这里有一个很好的解释https://www.stevefenton.co.uk/2011/04/asp-net-mvc-post-redirect-get-pattern/ – Fran

相关问题