I have this on my model
[Required]
public string AppName { get; set; }
on my layout page i have
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.24.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
my view has this inside BeginForm
@Html.EditorFor(model => model.App.AppName, new { size = 35 })
@Html.ValidationMessageFor(model => model.App.AppName)
<input type="submit" value="Start Business" class="demo-button ui-state-default ui-corner-all" />
this is how its rendered
<input id="App_AppName" class="text-box single-line watermark" type="text" value="" name="App.AppName" data-val-required="The AppName field is required." data-val="true">
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="App.AppName"></span>
when i hit submit i do not get any validation saying AppName is Required but it just post-backs to server with empty field.
i added few field as hidden with validationMsgFor and that is working.
so what i have is
public class AppContent
{
public App App { get; set; }
public bool CreateModal { get; set; }
public AppContent()
{
}
}
and my view is of type AppContent.
one thing i noticed was when i just add a view with App object directly AppName is detected as required. but when i do it using AppCOntent which has type AppName the validation is not shown, but for other hidden fields that are non-string it works.
You have to enable client validation in your web.config.
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
你必须把这个脚本
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
因为我有水印,我不得不使用占位符。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.