如何正确使用@ Html.BeginForm()?

时间:2019-04-18 12:36:10

标签: c# asp.net-core razor

我尝试使用Html.BeginForm()帮助器创建一个表单。但这会引发异常:

XmlException: Root element is missing.
System.Xml.XmlTextReaderImpl.Throw(Exception e)

CryptographicException: An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information.
Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Protect(byte[] plaintext)

这是标准的ASP.Net MVC应用程序。我更改了控制器代码,添加了视图模型和视图类。就是这样。

控制器:

public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Login(UserVM userVM)
        {
            return Json(userVM);
        }
    }

ViewModel:

public class UserVM
    {
        [Required]
        [EmailAddress]
        public string Email { get; set; }

        [Required]
        public string Password { get; set; }


    }

查看:

@using WebApplication1.ViewModels
@model UserVM

@using (Html.BeginForm("Login", "Home", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.TextBoxFor(m=>m.Email)
    @Html.PasswordFor(m=>m.Password)
}

这是我得到的: Screenshot of exception page

我做错了什么?

0 个答案:

没有答案