验证 MVC 4 中的部分视图
本文关键字:视图 MVC 验证 | 更新日期: 2023-09-27 18:32:11
我一直在尝试使用Data annonations
来验证我在bootstrap modal dialog
上显示的partial view
上的form
。以下是代码
部分视图
@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。 并且模式对话框不验证。
是什么原因导致行为?
不要在你的帖子中使用FormCollection
。试试这个:
[HttpPost]
public ActionResult Index([Bind(Include="ITXT_USER_ID ,TXT_USER_PWD ")]tbl_userMetaData userMetaData)
{
if (ModelState.IsValid)
{
RedirectToAction("Index", new { Area = "Admin" });
}
return View();
}
最好指定要绑定的属性。