Integrating Twitter Bootstrap with Asp.net MVC 3 forms

后端 未结 6 1727
有刺的猬
有刺的猬 2021-01-31 09:59

I am using Asp.net MVC 3 and Twitter Bootstrap. What I want is to integrate both of them. The big problem for me is forms. I am using the HtmlHelper and it is a pro

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-31 10:26

    I took the liberty and created an extension method that encapsulates the code from Sherbrow to render the error class when the field is not valid. This extension method has the advantage that it is strongly-typed and shorter to write:

    public static MvcHtmlString ModelStateFor(this HtmlHelper html, Expression> expression)
        {
            var modelMetadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
    
            if (html.ViewData.ModelState.IsValidField(modelMetadata.PropertyName))
            {
                return MvcHtmlString.Empty; 
            }
    
            return new MvcHtmlString("error");
        }
    

提交回复
热议问题