Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 496547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:41:47+00:00 2026-05-13T05:41:47+00:00

A user submits registration information to the form: <h2>Create</h2> <%= Html.ValidationSummary(Create was unsuccessful. Please

  • 0

A user submits registration information to the form:

<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>

<% } %>

Controller:

[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();

    }

controller factory:

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
}

Through the controller with an invalid value at first, then a valid one, but the error message stays and modelstate stays invalid. Why does this happen? i’m using the default model binder, but the included controller factory.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:41:48+00:00Added an answer on May 13, 2026 at 5:41 am

    A Controller should be instantiated per request. Think of the ControllerContext being much like the HttpContext. It represents the state of a single request. Since you’re storing controllers in a static dictionary, you’re not clearing the state of the controller for each request, thus it holds onto its modelstate.

    In general, you should not use the Singleton pattern with controllers because of this. Otherwise you’re responsible for clearing its state on each request. You’re probably not getting much of a perf benefit anyways by doing that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create simple user registration form. I have an index.html file
I have a registration form where this CSS rules apply. when user submits with
I have simple registration form. Once all information entered, user click submit button and
I'm creating a user registration form and after they fill in all the information
For example: A user submits a form with a <textarea name=mytext cols=35 rows=2></textarea> and
I have form where user submits field. Field can have letters, numbers, and punctuation.
After a user submits a form, how do you detect that the form has
I have a form that the user submits and returns a result, but it
I have situation like this: user submits form with action='/pay' in '/pay' I have
I need to hide a form on my page once a user submits it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.