2009-12-02 54 views
0

用戶提交登記信息發送到形式:爲什麼驗證錯誤仍然存​​在?

<h2>Create</h2> 

<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %> 

<% using (Html.BeginForm()) {%> 

    <fieldset> 
     <legend>Fields</legend> 
     <p> 
      <label for="Nbk">Nbk:</label> 
      <%= Html.TextBox("Nbk",null, new {Disabled="Disabled" })%> 
      <%= Html.ValidationMessage("Nbk", "*") %> 
     </p> 
     <p> 
      <label for="Name">Name:</label> 
      <%= Html.TextBox("Name") %> 
      <%= Html.ValidationMessage("Name", "*") %> 
     </p> 
     <p> 
      <label for="Email">Email:</label> 
      <%= Html.TextBox("Email") %> 
      <%= Html.ValidationMessage("Email", "*") %> 
     </p> 
     <p> 
      <label for="MailCode">MailCode:</label> 
      <%= Html.TextBox("MailCode") %> 
      <%= Html.ValidationMessage("MailCode", "*") %> 
     </p> 
     <p> 
      <label for="TelephoneNumber">TelephoneNumber:</label> 
      <%= Html.TextBox("TelephoneNumber") %> 
      <%= Html.ValidationMessage("TelephoneNumber", "*") %> 
     </p> 
     <p> 
      <label for="OrganizationId">OrganizationId:</label> 
      <%= Html.TextBox("OrganizationId") %> 
      <%= Html.ValidationMessage("OrganizationId", "*") %> 
     </p> 
     <p> 
      <label for="OrganizationSponsorId">OrganizationSponsorId:</label> 
      <%= Html.TextBox("OrganizationSponsorId") %> 
      <%= Html.ValidationMessage("OrganizationSponsorId", "*") %> 
     </p> 
     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 

<% } %> 

控制器:

[Authorize] 
    [AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult Create(DefectSeverityAssessmentBusiness.ModelRegistration registration) 
    { 
     registration.Nbk = StateController.GetNbk(Request); 
     try 
     { 
      var errors = DataAnnotationsValidationRunner.GetErrors(registration); 
      if (errors.Any()) 
       foreach (var item in errors) 
       { 
        if(ModelState[item.PropertyName].Errors.Count==0) 
        ModelState.AddModelError(item.PropertyName, item.ErrorMessage); 
        //ModelState.SetModelValue(item.PropertyName,ViewData[item.PropertyName].ToValueProvider()); 
       } 
      if (ModelState.IsValid) 
      { 
       _RegistrationRepository.CreateRegistration(registration); 
       return RedirectToAction("Index", "Assessment"); 
      } 
     } 
     catch (Exception exception) 
     { 
      ModelState.AddModelError("Exception", exception); 
     } 


     return View(); 

    } 

控制器工廠:

ControllerBuilder.Current.SetControllerFactory(new Models.InMemoryRepositories.InMemoryControllerFactory()); 

public class InMemoryControllerFactory : IControllerFactory 
{ 

    private readonly Dictionary<string, IController> _controllers = new Dictionary<string, IController>(); 

    private readonly Dictionary<string, Func<IController>> _controllerFactoryDictionary = 
     new Dictionary<string, Func<IController>>(); 

    public InMemoryControllerFactory() 
    { 
     InitializeDictionary(); 
    } 

    private void InitializeDictionary() 
    { 

     AddFactory(typeof(Controllers.HomeController),() => new Controllers.HomeController(
      new Models.InMemoryRepositories.Registration.InMemoryRegistrationRepository())); 
     AddFactory(typeof(Controllers.RegistrationController),() => new Controllers.RegistrationController(
       new Models.InMemoryRepositories.Registration.InMemoryRegistrationRepository())); 
     AddFactory(typeof(Controllers.AssessmentController),()=> new Controllers.AssessmentController(
      new Models.InMemoryRepositories.Registration.InMemoryDefectRepository(), 
      new Models.InMemoryRepositories.Registration.InMemoryAssessmentRepository()) 
      ); 
    } 

    private void AddFactory(Type type, Func<IController> creator) 
    { 
    const string Str_Controller = "Controller"; 
       var fullname = type.Name; 
       Debug.Assert(fullname.EndsWith(Str_Controller)); 
       var controllerName= fullname.Substring(0, fullname.Length - Str_Controller.Length); 

     Func<string, Func<IController>> controllerFactoryFunc = (_controllerName) => 
      { 
       return() => 
        { 
         //var controllerName=ControllerNameFunc(type); 
         if (!_controllers.ContainsKey(_controllerName)) 
          _controllers.Add(_controllerName, creator()); 
         return _controllers[_controllerName]; 
        }; 
      }; 

     _controllerFactoryDictionary.Add(controllerName, controllerFactoryFunc(controllerName)); 
    } 


    #region IControllerFactory Members 



    public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName) 
    { 
     return _controllerFactoryDictionary[controllerName](); 
    } 

    /// <summary> 
    /// Code via http://nayyeri.net/custom-controller-factory-in-asp-net-mvc 
    /// </summary> 
    /// <param name="controller"></param> 
    public void ReleaseController(IController controller) 
    { 

     if (controller is IDisposable) 
      (controller as IDisposable).Dispose(); 
     else 
      controller = null; 
    } 

    #endregion 
} 

通過與一個無效值時,控制器在第一,則有效其中一個,但錯誤消息仍然存在,並且modelstate保持無效。爲什麼會發生?我使用默認的模型綁定器,但包含的控制器工廠。

+0

在調試器中查看控制器中的創建操作時,註冊值在其返回時不會更新。調用'UpdateModel (註冊)'我得到*'DefectSeverityAssessmentBusiness.ModelRegistration'沒有成功更新。* – Maslow 2009-12-02 21:00:53

回答

2

控制器應該根據請求實例化。認爲ControllerContext很像HttpContext。它代表單個請求的狀態。由於您將控制器存儲在靜態字典中,因此您沒有清除每個請求的控制器狀態,因此它保留在其模型狀態中。

一般來說,你不應該在控制器中使用Singleton模式。否則,您需要負責清理每個請求的狀態。無論如何,你可能沒有獲得太多的表現。

+0

我想這就是問題所在,我會盡快嘗試一下。 – Maslow 2009-12-04 20:33:19

相關問題