Im trying to do a little render framework, since I need some more control over the render process. Fx. if a property need to be rendered in a tab.
So I set out to, render a TextBox, but it does not validate with server side or client side validation (the MVC unobtrusive validation)
I have taken my framework out, and recreated a little eksampel
public class Foo
{
public virtual int Id { get; set; }
[System.ComponentModel.DataAnnotations.Required]
public virtual string Name { get; set; }
public virtual DateTime StartTime { get; set; }
}
My extension method:
public static MvcHtmlString DummyForm(this HtmlHelper html)
{
StringBuilder sb = new StringBuilder();
Type oftype = typeof(Foo);
string[] propertyNameToRender = oftype.GetProperties().Select(o => o.Name).ToArray();
foreach (string s in propertyNameToRender)
{
MvcHtmlString htmlstring = System.Web.Mvc.Html.InputExtensions.TextBox(html, s);
sb.AppendLine(htmlstring.ToHtmlString());
}
return MvcHtmlString.Create(sb.ToString());
}
And on the Edit.cshtml
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
开发者_JS百科 @Html.ValidationSummary(true);
@Html.DummyForm()
}
If I look at the rendered html output, its the same (without the validation attri) Can anyone tell me, why the validation attri, is not rendered.
Im using the mvc's own render controls, HtmlHelper is passed from the view, with all ModelMetadata and ModelState.
Unobtrusive validation data-val-*
attributes are rendered when FormContext
is initialized. Html.BeginForm
does that, so
@using (Html.BeginForm())
{
@Html.DummyForm()
}
Should render inputs with validation attributes.
There is one thing that seems odd is that you are calling System.Web.Mvc.Html.InputExtensions.TextBox
method yourself. This method is internally called by Html.TextBox
and other strongly typed extensions. plz try changing
MvcHtmlString htmlstring = System.Web.Mvc.Html.InputExtensions.TextBox(html, s);
to
MvcHtmlString htmlstring = html.TextBox(s);
I have try to create a new ASP.net MVC site, and added the code from my RenderProject, and it works fine. The conclusion is my asp.net MVC project messed up. I dont why. :S
精彩评论