我正在使用Smart Admin在MVC中开发一个应用程序,我在其中使用数据注释进行验证。我的代码是:
public class EmployeeDTO
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "Name is required")]
[StringLength(50, ErrorMessage = "Name can accept maximum 50 characters.")]
[RegularExpression("^([a-zA-Z]{1}[a-zA-Z '-.(-,)-/&]*)$", ErrorMessage = "Please enter valid name.")]
public string Name { get; set; }
}
我的观点是
<script src="~/SmartAdmin/js/libs/jquery-2.0.2.min.js"></script>
<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>
@model HBSeworld.Admin.DTO.EmployeeDTO
@{
ViewBag.Title = "Create";
}
<section class="jarviswidget" id="wid-id-1" data-widget-editbutton="false" data-widget-custombutton="false">
<header>
<span class="widget-icon"><i class="fa fa-user"></i></span>
<h2>Create Employee</h2>
</header>
<div>
<div class="jarviswidget-editbox">
</div>
<section class="widget-body no-padding">
@using (Html.BeginForm("Create", "Employee", FormMethod.Post, new { @id = "order-form", enctype = "multipart/form-data", @class = "smart-form" }))
{
@Html.ValidationSummary(true)
<fieldset>
<div class="row">
<section class="label col col-2" style="text-align: right">
@Html.LabelFor(model => model.Name)
</section>
<section class="col col-4">
<label class="input">
<i class="icon-append fa fa-user"></i>
@Html.TextBoxFor(model => model.Name, null, new { @placeholder = "Enter Name" })
@Html.ValidationMessageFor(model => model.Name)
</label>
</section>
</div>
</fieldset>
<div class="pull-right" style="margin-right: 5px; margin-bottom: 10px;">
<input type="submit" id="create" value="Create" class="btn btn-primary" style="height: 25px; width: 45px;" />
<input type="button" style="height:25px; width:45px;" value="Cancel" class="btn btn-default" onclick="window.location.href='@Url.Action("index") '"/>
</div>
}
</section>
</div>
</section>
现在,我的问题是,当我点击Create
按钮时,数据注释消息会正确显示,但同时控件会转到控制器,而应该在同一个视图上,并且必须显示数据注释消息。怎么预防这个?任何解决方案?