Answer SLaks is right, but no additional information is available.
If you want to use traditional (not unobtrusive) client-side validation, you need to provide formContext and provide it with an identifier so that you can verify client-side validation. In his explanation, this part is missing.
The easiest way to achieve this is to use a return instance of the MvcForm class that creates the formContext and implements the IDisposable interface.
In this implementation, I needed to specify the form identifier:
public static MvcForm BeginFormDatosAdicionales(this HtmlHelper htmlHelper, string id, ..., IDictionary<string, object> htmlAttributes = null) { TagBuilder form = new TagBuilder("form"); // attributes form.MergeAttributes(htmlAttributes); // action string formAction = ...; form.MergeAttribute("action", formAction); // method FormMethod method = ...; form.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true); // id form.MergeAttribute("id", id); // writes the form opening tag in the ViewContext.Writer htmlHelper.ViewContext.Writer.Write(form.ToString(TagRenderMode.StartTag)); // creates an MvcForm (disposable), which creates a FormContext, needed for // client-side validation. You need to supply and id for it MvcForm theForm = new MvcForm(htmlHelper.ViewContext); htmlHelper.ViewContext.FormContext.FormId = form.Attributes["id"]; // The returned object implements IDisposable, and writes the closing form tag return theForm; }
Of course, this can be customized for your particular case. If you only want to provide an identifier for your form when it is absolutely necessary, check this as follows:
bool idRequired = htmlHelper.ViewContext.ClientValidationEnabled && !htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled;
In this case, you must be careful to create different identifiers for each form on the page. For example, you can add an integer suffix that can be stored in HttpContext.Items and incremented each time a new identifier is generated. This ensures that on the same page all identifiers created are different.
HttpContext.Current.Items["lastFormId"]
Jotabe
source share