【问题标题】:Validate Partial View in mvc 4在 mvc 4 中验证局部视图
【发布时间】:2016-01-28 08:52:35
【问题描述】:

我一直在尝试使用Data annonations 来验证我的partial view 上的form,我在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。和模态对话框不验证。

导致这种行为的原因是什么?

【问题讨论】:

  • 您的 POST 方法没有您的模型的参数(只是 FormCollection 的一个参数 - 您永远不应该使用它)所以没有 ModelState 错误
  • 你能详细说明你的答案吗?我仍然是 MVC 的新手并正在学习它。我想如果两个字段都没有验证,模式对话框不应该关闭并显示错误。
  • 您的方法需要是[HttpPost] public ActionResult Index(tbl_user model)。而且您不会在客户端中获得任何验证,因为您没有包含任何与您的属性关联的@Html.ValidationMessageFor()

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


【解决方案1】:

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

}

最好指定要绑定的属性。

【讨论】:

  • 它显示ModalState 无效,但模式对话框在表单的后部关闭。
  • 指定要绑定的属性不是好的做法。好的做法是使用视图模型,因此永远不需要[Bind] 属性。跨度>
猜你喜欢
  • 1970-01-01
  • 2013-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多