未收到View到Controller的值

时间:2015-03-06 09:32:17

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

我的观点:

 @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方法中设置断点时,我看到所有三个参数都为null,即使它们是在文本框中提供的。我错过了什么我也可以添加模型代码,但我想这还不相关,因为它尚未涉及。

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

您好。我在视图中引入了一个提交按钮,并删除了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 ...

1 个答案:

答案 0 :(得分:2)

您只需使用普通输入按钮即可使用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");            
}