未收到从视图到控制器的值

Values from View to Controller not recieved

我的看法:

 @using (Html.BeginForm("RegisterNewUser ", "RegisterUser", new { @class = "registerForm" }))
{
    @Html.LabelFor(model => model.nickName)
    @Html.TextBoxFor(model => model.nickName, new { @class = "RegisterControls" })
    <br /><br />

    @Html.LabelFor(model => model.email)
    @Html.TextBoxFor(model => model.email, new { @class = "RegisterControls" })
    @Html.ValidationMessageFor(model => model.email)
    <br /><br />

    @Html.LabelFor(model => model.password)
    @Html.PasswordFor(model => model.password, new { @class = "RegisterControls", id = "firstPassword" })
    <br /><br />

    @Html.LabelFor(model => model.confirmPassword)
    @Html.PasswordFor(model => model.password, new { @class = "RegisterControls" })
    @Html.ValidationMessageFor(model => model.confirmPassword)
    <br /><br /><br />
    <hr style="color: #D5E0EE; border: solid; border-style: double;" />

    @Ajax.ActionLink("Create account", "RegisterNewUser", "RegisteredUsers", new AjaxOptions { UpdateTargetId = "updaterDiv", HttpMethod = "POST" }, new { @class = "actionButtons" })
    <br /><br />

我的控制器:

 [HttpPost]
    public ActionResult RegisterNewUser(string uName, string email, string password)
    {
        ViewBag.Message = "Register";
        string msg = "";

        if (!users.RegisterUser(uName, email, password))
        {
            msg = "The provided email already exists";
        }

        ViewBag.Message = msg;
        return PartialView("_Register");            
    }

当我在 Action 方法中设置断点时,我发现所有三个参数均为空,即使在文本框中提供了它们也是如此。我错过了什么。我也可以添加模型代码,但我想这还不相关,因为它还没有涉及。

------------------------------------更新------ --------------------------

你好。我在我的视图中引入了一个提交按钮,并删除了 Ajax ActionLink。所以视图现在包含这个:

<input type="submit" value="Register" onclick="@Url.Action("RegisterNewUser", "RegisterUsers");" class="actionButtons" />

并且我把Action方法的参数改成了一个模型,所以现在看起来是这样的:

[HttpPost]
    public ActionResult RegisterNewUser(RegisteredUsers registerUserModel)
    {
        ViewBag.Message = "Register";
        string msg = "";

        if (!users.RegisterUser(registerUserModel.nickName, registerUserModel.email, registerUserModel.password))
        {
            msg = "The provided email already exists";
        }
        return PartialView("_Register");            
    }

问题是一样的,值仍然为空。这次设置断点时,我注意到提交按钮没有调用 Action 方法。单击提交按钮实际上什么也不做。我也尝试过不添加 onclick 事件侦听器,就像我在一些示例中看到的那样,但结果保持不变。

如果需要,模型如下所示:

public class RegisteredUsers
{
    private DBLogic logic;

    [Display(Name ="Your Email")]
    [Required(ErrorMessage="You must provide a valid Email")]
    [EmailAddress]
    public string email { get; set; }

    [Required(ErrorMessage="Select a user name", AllowEmptyStrings = false)]
    [Display(Name="User name")]
    public string nickName { get; set; }

    [Required(ErrorMessage="Select a password", AllowEmptyStrings = false)]
    [Display(Name="Password")]
    [DataType(DataType.Password)]
    public string password { get; set; }

    [Required(ErrorMessage="Type your password again")]
    [Display(Name="Confirm password")]
    [DataType(DataType.Password)]
    [Compare("Password", ErrorMessage="The two passwords do not match")]
    public string confirmPassword { get; set; }

    [Display(Name="Remember me")]
    public bool rememberUserOnThisComputer { get; set; }
public void ...

您应该使用 Ajax.BeginForm 而不是仅使用普通输入按钮。

@using (Ajax.BeginForm("RegisterNewUser ", "RegisterNewUser ", new AjaxOptions { UpdateTargetId = "result" }))    
{
  <div id="result"></div>
  ...form data
  <input type="submit" value="Ajax Form Action" />
}

您还可以更改控制器方法以将模型对象作为参数。 这样您就不需要指定每个参数。

没有看到你的模型是这样的:

[HttpPost]
public ActionResult RegisterNewUser(RegisterModel model)
{
    ViewBag.Message = "Register";
    string msg = "";

    if (!users.RegisterUser(model.uName, model.email, model.password))
    {
        msg = "The provided email already exists";
    }

    ViewBag.Message = msg;
    return PartialView("_Register");            
}