我一直在嘗試使用Data annonations
來驗證form
我的partial view
,我在bootstrap modal dialog
上顯示。以下是代碼驗證在mvc中的部分視圖4
管窺
@model JoyRydeStoreWebPortal.Models.tbl_user
<div class="modal fade" id="createLoginModal" aria-hidden="false" role="dialog" tabindex="-1">
<div class="modal-dialog">
<form class="modal-content" method="post" action="@Url.Action("Index","Index", new{ Area = "Admin" })">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="createLoginTitle">Create Login</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-lg-4 form-group">
@Html.TextBoxFor(modal => modal.TXT_USER_ID, new { placeholder = "UserID", id="userID" , @class="form-control"})
</div>
<div class="col-lg-4 form-group">
@Html.TextBoxFor(modal => modal.TXT_USER_PWD, new {type="Password", placeholder = "Password", id = "userPwd", @class = "form-control" })
</div>
<div class="col-sm-12 pull-right">
<button class="btn btn-primary btn-outline" type="submit">Create</button>
</div>
</div>
</div>
</form>
</div>
索引視圖我在哪裏rendering
局部視圖
@model List<JoyRydeStoreWebPortal.Models.tbl_store>
@using GridMvc.Html;
@{
ViewBag.Title = "Index";
Layout = "~/Areas/Admin/Views/Shared/_layoutAdmin.cshtml";
}
<div class="page">
<div class="page-content padding-30 container-fluid">
@Html.Grid(Model).Columns(columns =>
{
columns.Add(foo => foo.LNG_STORE_ID,true).Sortable(true);
columns.Add(foo => foo.TXT_STORE_NAME).Titled("Store Name").SetWidth(110).Sortable(true).Filterable(true);
columns.Add(foo => foo.TXT_STORE_CONTACT).Titled("Phone Number").SetWidth(110).Sortable(true).Filterable(true);
columns.Add() .Encoded(false).Sanitized(false).SetWidth(30).RenderValueAs(o => @<a href="#createLoginModal" data-toggle="modal">Create Login</a>);
columns.Add().Encoded(false).Sanitized(false).SetWidth(30).RenderValueAs(o => @<a href="#">Detail</a>);
}).WithPaging(20)
</div>
<div>
@{Html.RenderPartial("Partial_CreateLogin", new JoyRydeStoreWebPortal.Models.tbl_user());}
</div>
以下是我的模態班TBL_USER
通過Entity Framework
namespace JoyRydeStoreWebPortal.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
[MetadataType(typeof(tbl_userMetaData))]
public partial class tbl_user
{
public long LNG_USER_ID { get; set; }
public string TXT_USER_ID { get; set; }
public string TXT_USER_PWD { get; set; }
public long LNG_STORE_ID { get; set; }
public virtual tbl_store tbl_store { get; set; }
}
}
tbl_userMetaData類創建
public class tbl_userMetaData
{
[Required(ErrorMessage = "User ID is required")]
public string TXT_USER_ID { get; set; }
[Required(ErrorMessage = "User Password is required")]
public string TXT_USER_PWD { get; set; }
}
指數控制器
[HttpGet]
public ActionResult Index()
{
using (joyryde_storeEntities context = new joyryde_storeEntities())
{
var items = context.tbl_store.ToList();
return View(items);
}
}
[HttpPost]
public ActionResult Index(FormCollection frm)
{
if (ModelState.IsValid)
{
RedirectToAction("Index", new { Area = "Admin" });
}
return View();
}
這裏ModalState.IsValid
總是返回true。和模式對話框不驗證。
是什麼導致了這種行爲?
你的POST方法沒有你的模型的參數(只是'FormCollection'的一個參數 - 你永遠不應該使用它),所以沒有'ModelState'錯誤 –
請你詳細說明你的答案。我仍然是MVC中的新手並學習它。我希望如果兩個領域都沒有驗證,模態對話框不應該關閉並顯示錯誤。 –
您的方法需要是'[HttpPost]公共ActionResult索引(tbl_user模型)'。你不會在客戶端得到任何驗證,因爲你沒有包含任何與你的屬性相關的@ Html.ValidationMessageFor()。 –