簡體   English   中英

使用POST方法的MVC 4發布不起作用(使用GET方法-有效)

[英]MVC 4 posting with POST method doesn't work (with GET method - works)

這是我的模型:

public class LoginModel {
    public string username { get; set; }
    public string password { get; set; }
    public string ReturnUrl { get; set; }
}

這是我的控制器標頭:

    [AllowAnonymous]
    public ActionResult Login(
        LoginModel model
    )

這是我的看法:

 <form action="@Url.Action("Login", "Login")" method="GET">
        @Html.HiddenFor(m=>m.ReturnUrl)
        User name: @Html.TextBoxFor(f=>f.username)
        <br />
        Password: @Html.PasswordFor(f=>f.password)
        <br /><br />
        <button type="submit">Login</button>
    </form>

當我將表單上的方法從“ GET”更改為“ POST”時,綁定不起作用。 我嘗試添加:

[AcceptVerbs("POST", "GET")]

到控制器頭,這無濟於事

請注意, ActionResult Login不是您的控制者……這是您的操作,請將其添加到操作的標題中:

[HttpPost] 
public ActionResult Login(LoginModel model)
{
  // your code
  return View("NameofView"); //**UPDATE
}

和您的表格:

method="POST"

UPDATE *

看到您可以在return子句中顯式設置視圖的名稱。 而且,如果POST正在調用登錄操作,那是因為您將其明確地放在表單定義內的action屬性中。

<form action="<Here_your_action>" method="POST">

如果要對GET和POST都執行“操作”,則必須編寫兩種格式來匹配此定義。

您可能應該使用:

@using (Html.BeginForm())
{
  @Html.TextBoxForm(model => model.MyField)
}

而且,正如Max所說,請使用return View(); 在您的行動中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM