0

单击名为“保存”的按钮时未命中注册操作?:

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);
        }
    }
4

2 回答 2

0

您没有将任何玩家对象发送到您的视图。你需要这样的东西:

 public ActionResult RegisterPlayer()
        {
            Player player=new Player();
            return View(player);
        }

我还建议您使用调试器查看它崩溃的位置。这显然是一个数据传输问题。如果您不知道如何使用调试器访问视图,请使用 F11 浏览它

于 2012-06-01T07:35:05.603 回答
0

我认为问题在于您的valueinput您需要的操作不匹配。

试试这个:

<input type="submit" value="RegisterPlayer" />
于 2012-06-01T07:37:02.340 回答