【问题标题】:ASP.NET MVC 4 Validation Errors not appearingASP.NET MVC 4 验证错误未出现
【发布时间】:2012-12-05 07:36:22
【问题描述】:

我所有的路线:

    routes.MapRoute(
        name: "Login",
        url: "{eid}/Login",
        defaults: new { controller = "Account", action = "Login", eid = ConfigurationManager.AppSettings["Congress_Code"] } // Parameter defaults
    );

    routes.MapRoute(
        name: "Account",
        url: "{eid}/Account/{action}",
        defaults: new { controller = "Account", action = "{action}", eid = ConfigurationManager.AppSettings["Congress_Code"] }
    );

    routes.MapRoute(
        name: "Default",
        url: "{eid}/{controller}/{action}",
        defaults: new { controller = "Account", action = "Login", eid = ConfigurationManager.AppSettings["Congress_Code"] }
    );

在manage.cshtml中:

@using (Html.BeginForm(new { eid = ViewBag.EventId }))
{
    @Html.ValidationSummary(false)

在帐户控制器中:

//
// GET: /Account/Manage
//[AllowAnonymous]
public ActionResult Manage(int eid, ManageMessageId? message)
{
    ViewBag.StatusMessage =
        (message == ManageMessageId.UpdateDetailsObjSuccess) ? "Your user details have been modified."
        : "";
    return View();
}

//
// POST: /Account/Manage
[HttpPost]
public ActionResult Manage(UpdateUserDetailsModel model,int eid)
{
    if (ModelState.IsValid)
    {
        var updateModel = AutoMapper.Mapper.Map<Models.UpdateUserDetailsModel,KPAD_Api.Kiosk.KioskUserDetailsExtended>(model);
        try
        {
            long token = Proxy.Proxy.Instance.Token[KioskUser.Id];
            var result = Proxy.Proxy.Instance.KioskUserClient.UpdateKioskUser(updateModel, token);
            if (result.ErrorCode == ReturnCodes.Codes.ALL_OK)
                return RedirectToAction("Manage", new { Message = ManageMessageId.UpdateDetailsObjSuccess });
            else
                ModelState.AddModelError(ReturnCodes.Instance.GetMessage(result.ErrorCode), new Exception(ReturnCodes.Instance.GetMessage(result.ErrorCode)));
        }
        catch (Exception e)
        {
            ModelState.AddModelError("Database error occured.Please contact the website administrator", e);
        }
    }
    else
    {
        ModelState.AddModelError("An unknown error occurend. Please try again. If it persists please contact the admin.",new Exception("Unknown"));
    }
    // If we got this far, something failed, redisplay form
    return View(model);
}

但是当我故意生成异常或自然发生异常时,验证摘要仍然没有显示。 有人有什么想法吗?

【问题讨论】:

    标签: asp.net-mvc validation validationsummary


    【解决方案1】:

    试试这个

    在manage.cshtml中:

    @using (Html.BeginForm(new { eid = ViewBag.EventId }))
    {
        @Html.ValidationSummary(true)
    

    在控制器中

    [HttpPost]
    public ActionResult Manage(UpdateUserDetailsModel model,int eid)
    {
        if (ModelState.IsValid)
        {
            var updateModel = AutoMapper.Mapper.Map<Models.UpdateUserDetailsModel,KPAD_Api.Kiosk.KioskUserDetailsExtended>(model);
            try
            {
                long token = Proxy.Proxy.Instance.Token[KioskUser.Id];
                var result = Proxy.Proxy.Instance.KioskUserClient.UpdateKioskUser(updateModel, token);
                if (result.ErrorCode == ReturnCodes.Codes.ALL_OK)
                    return RedirectToAction("Manage", new { Message = ManageMessageId.UpdateDetailsObjSuccess });
                else
                    ModelState.AddModelError(ReturnCodes.Instance.GetMessage(result.ErrorCode), new Exception(ReturnCodes.Instance.GetMessage(result.ErrorCode)));
            }
            catch (Exception e)
            {
                ModelState.AddModelError("","Database error occured.Please contact the website administrator");
            }
        }
        else
        {
            ModelState.AddModelError("","An unknown error occurend. Please try again. If it persists please contact the admin.");
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }
    

    它绑定了键值对中的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      • 2012-06-26
      相关资源
      最近更新 更多