在mvc 4中验证部分视图

时间:2016-01-28 08:52:36

标签: c# asp.net-mvc validation partial-views

我一直在尝试使用Data annonations来验证form上我partial view上显示的bootstrap modal dialog。以下是代码

部分视图

@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。和模态对话框不验证。

造成这种行为的原因是什么?

1 个答案:

答案 0 :(得分:2)

不要在帖子上使用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();

}

最好指定要绑定的属性。