我有一个有2个属性的类
[StringLength(8, ErrorMessage = "User name can't be longer than 8 chars!")]
[Required(ErrorMessage = "Must be filled!")]
public string NickName { get; set; }
[Required(ErrorMessage = "Must be filled!")]
[DataType(DataType.Password)]
public string Password { get; set; }
我有这个局部视图,用户插入他的详细信息并按下sumbit按钮。
NickName:
@Html.TextBoxFor(x => x.NickName)
@Html.ValidationMessageFor(x => x.NickName)
</div>
<div>
Password:
@Html.TextBoxFor(x => x.Password)
@Html.ValidationMessageFor(x => x.Password)
</div>
<button type="submit">Log In</button>
这是我的邮政行动,
[HttpPost]
public ActionResult LogIn(ShortUser u)
{
if (ModelState.IsValid == true)
{
if (u.PassMatchUser())
{
FormsAuthentication.SetAuthCookie(u.NickName, true);
}
else
{
ModelState.AddModelError("Error","Username / Password inncorect!");
}
return Redirect(ControllerContext.HttpContext.Request.UrlReferrer.ToString());
}
else
{
return Redirect(ControllerContext.HttpContext.Request.UrlReferrer.ToString());
}
}
如果我插入无效值,则不会显示验证文本。
答案 0 :(得分:0)
您需要将验证摘要设置为
@Html.ValidationSummary(true)
@Html.EnableClientValidation()
在您的部分视图上。
答案 1 :(得分:0)
将其围绕在表单块中。
<?php
use Illuminate\Http\Request;
// Route::middleware('auth:api')->get('/user', function (Request $request) {
// return $request->user();
// });
Route::Resource('test/client', 'ClientsController', ['only' => ['index', 'show', 'store']]);
Route::Resource('test/sender', 'SenderController', ['only' => ['index','show', 'store', 'destroy']]);
Route::Resource('test/template', 'TemplateController', ['only' => ['index','show', 'store', 'destroy']]);
Route::Resource('test/message', 'MessageController', ['only' => ['index','show', 'store']]);
Route::get('/userdetail', function (Request $request) {
return $request->user();
})->middleware(['auth:api', 'scopes:manage-SMS']);
或
<form>
. . .
</form>
并确保您正在加载jquery,jquery.validate和jquery.validate.unobtrusive
答案 2 :(得分:0)
ModelState没有真正的要求 改变
if(ModelState.IsValid==true) -> if(ModelState.IsValid)
确保以下内容:
如果ModelState无效,则返回视图。
返回View();
视图中的模型和在Post Action中返回的模型应该相同。
确保您正在加载jquery,jquery.validate和jquery.validate.unobtrusive
<script src="@Url.Content("~/Scripts/jquery.validate.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")">
@Scripts.Render("~/bundles/jqueryval")
答案 3 :(得分:0)
将部分视图代码放在beginform
中例如: @using(Html.BeginForm(“LogIn”,“你的控制器名称”,FormMethod.Post)) {
///部分查看代码
}