Reputation: 1782
My View:
@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 />
My controller:
[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");
}
When I set a break point at the Action method, I see that all three parameters are null, even when they are provided in the textboxes. What am I missing. I could also add the model code, but I guess this is not yet relevant, as it's not involved yet.
-------------------------------------Update----------------------------------
Hi. I have introduced a submit button to my view, and removed the Ajax ActionLink. So the view now contains this:
<input type="submit" value="Register" onclick="@Url.Action("RegisterNewUser", "RegisterUsers");" class="actionButtons" />
And I changed the parameter of the Action method to take a model, so it now looks like this:
[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");
}
The problem is the same, the values are still null. This time when setting a breakpoint, I notice that the submit button is not calling the Action method. The submit button actually does nothing when clicked. I have also tried not adding the onclick event listener as I see in some examples, but result remains the same.
In case it is needed, the model looks like this:
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 ...
Upvotes: 1
Views: 48
Reputation: 33306
You should use an Ajax.BeginForm
instead with just a normal input button.
@using (Ajax.BeginForm("RegisterNewUser ", "RegisterNewUser ", new AjaxOptions { UpdateTargetId = "result" }))
{
<div id="result"></div>
...form data
<input type="submit" value="Ajax Form Action" />
}
You could also change your controller method to take the model object as the parameter. This way you would not need to specify each parameter.
Without seeing your model something like this:
[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");
}
Upvotes: 2