单击名为“保存”的按钮时未命中注册操作?:
http://localhost/Account/RegisterPlayer
控制器:
public ActionResult RegisterPlayer()
{
return View();
}
[HttpPost]
public ActionResult RegisterPlayer(Player player)
{
//it does not hit this action : (
if (ModelState.IsValid)
{
}
return View(player);
}
注册播放器.cshtml
@model mystuff.Models.Player
@{
ViewBag.Title = "RegisterPlayer";
}
<h2>RegisterPlayer</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Player</legend>
@Html.HiddenFor(model => model.id)
<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.email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.email)
@Html.ValidationMessageFor(model => model.email)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
玩家等级:
public class Player
{
public int id { get; set; }
public string firstname { get; set; }
public string email { get; set; }
}
全球.asax
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}