列名称“长度”无效的webmatrix

时间:2015-10-14 03:58:09

标签: c# asp.net-mvc razor webmatrix

我在asp.net mvc中的新功能。我看了一个教程,他正在使用webmatrix。我试图在webmatrix创建的数据库中的表“UserProfile”中添加名为“IDViewer”的列。但是,当我试图插入一些值时,它返回一个错误“无效的列名'长度'。”但我没有在我的代码中声明列名“长度”。请检查我的代码和图像。谢谢

图片链接在这里。请看看

The error

My database, i added column named IDViewer

public class Register
{
    [Required(ErrorMessage = "Please provide password", AllowEmptyStrings = false)]
    public string Username { get; set; }
    [Required(ErrorMessage = "Please provide password", AllowEmptyStrings = false)]
    [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
    [StringLength(50, MinimumLength = 8, ErrorMessage = "Password must be 8 char long.")]
    public string Password { get; set; }
    public string IDViewer { get; set; }
}
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary("", new { @style = "color: red" })
    <label>Username</label>
    @Html.TextBoxFor(m => m.Username, new { @class = "form-control" })
    <label>Password</label>
    @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
    <label>Student ID</label>
    @Html.TextBoxFor(m => m.IDViewer, new { @class = "form-control" })
    @Html.DropDownList("role",roles,"Select Account Type")
    <button class="btn btn-primary">Register</button>
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(Register registerdata, string role)
{
    if(ModelState.IsValid)
    {
        try
        {
            WebSecurity.CreateUserAndAccount(registerdata.Username, registerdata.Password, registerdata.IDViewer);
            Roles.AddUserToRole(registerdata.Username,role);
            return RedirectToAction("Index", "Home");
        }
        catch (MembershipCreateUserException)
        {
            ModelState.AddModelError("", "Sorry the username already exists");    
            return View(registerdata);
        }
    }
    ModelState.AddModelError("","Data invalid");
    return View(registerdata);
}

1 个答案:

答案 0 :(得分:1)

documentation开始,第3个参数是字典

  

propertyValues

     

类型:System.Object

     

(可选)包含其他用户属性的字典。默认值为null。

所以你的方法需要

WebSecurity.CreateUserAndAccount(registerdata.Username,
    registerdata.Password, new { IDViewer = registerdata.IDViewer });