rolling my own @Html.BeginfBrm()

前端 未结 2 824
花落未央
花落未央 2020-12-31 21:11

I am writing a custom validation set that will display all missing elements on a div. I\'d like to be able to use a custom @Html.BeginForm() method that will w

相关标签:
2条回答
  • 2020-12-31 21:19

    You need to write an extension method for the HtmlHelper class that prints to helper.ViewContext.Writer.

    The method should return an IDisposable that prints the closing tag in its Dispose method.

    0 讨论(0)
  • 2020-12-31 21:41

    SLaks answer is right, but is missing some extra information.

    If you want to use traditional (not unobtrusive) client side validation, you need to supply a formContext, and give it an id, so that the client side validation can work. In his explanation this part is missing.

    The easiest way to achieve this is to use return an instance of the MvcForm class, that creates the formContext, and implements the IDisposable interface.

    In this implementation I needed to supply the form's id:

    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's 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 id for your form when absolutely neccesary, check this contidition:

    bool idRequired = htmlHelper.ViewContext.ClientValidationEnabled
    && !htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled;
    

    In this case you must be careful to create different Ids for each form in a page. For example you can add an integer suffix, that can be stored in HttpContext.Items and incremented every time a new Id is generated. This ensures that in a single page all generated ids are different.

    HttpContext.Current.Items["lastFormId"]
    
    0 讨论(0)
提交回复
热议问题