Reputation: 85
I have a form with a submit button on a registration page that will not work when pressed, have been at it for a while now? I put a break point on the creat method and it doesn't get called
This is the Creat Metod in the controller
public ActionResult Create()
{
return View();
}
//
// POST: /RegsterDetails/Create
[HttpPost]
public ActionResult Create(User user )
{
if(ModelState.IsValid)
try
{
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("RegisterStats", "RegisterStats");
}
catch(Exception)
{
return View();
}
return View(user);
This is The View
@model Fitness_Friend.Web.Models.User
@{
ViewBag.Title = "RegisterDetails";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>RegisterDetails</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DOB)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DOB)
@Html.ValidationMessageFor(model => model.DOB)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Gender)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Gender)
@Html.ValidationMessageFor(model => model.Gender)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<p>
<input type="Submit" name="Create" value="Register Details" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index","Home")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Upvotes: 1
Views: 2082
Reputation: 1223
I'm very late to this but I think I know why this Controller Method doesn't get called.
If a form is passing the data for a Model back to the controller, then the Model class needs to have a constructor which takes no parameters. I'm willing to bet that in the above example the class User does not have such a constructor.
Upvotes: 0
Reputation: 54628
I've duplicated this code as below:
Controller + Model (bad design don't do this):
public class HomeController : Controller
{
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(MyUser user)
{
// break point next line works
if (ModelState.IsValid)
try
{
//db.Users.Add(user);
//db.SaveChanges();
return RedirectToAction("RegisterStats", "RegisterStats");
}
catch (Exception)
{
return View();
}
return View(user);
}
public class MyUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
With this view:
@model MvcApplication3.Controllers.HomeController.MyUser
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<p>
<input type="Submit" name="Create" value="Register Details" />
</p>
</fieldset>
}
And it works without issue.
Upvotes: 1
Reputation: 1989
For some reason, your form is not hooking up to your action properly. In order to eliminate some of the possibilities for what is going on, I suggest that you actually specify what Action and Controller the form will use to post to.
@Html.BeginForm("CreateUser", "ControllerWithCreateUserMethod")
{
//insert your HTML here.
//submit button here
<input type="submit" value="Submit"/>
}
Upvotes: 2
Reputation: 3204
In a HTML form, you need a submit button to post the data, like this
<input type="submit" value="Submit"/>
or
<button type="submit">Submit</button>
If you don't like those, you can choose something like this
<a onclick="$('#formId').submit();">Submit</a>
and of course you can optionally specify in form tag where the data is to be posted
@Html.BeginForm("Action", "Controller")
{
...
}
Upvotes: 1