MVC嵌套对象验证问题 [英] MVC nested object validation problem

查看:67
本文介绍了MVC嵌套对象验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个具有ID数据邮件等的用户。

Lets say I have an object User with ID dat mail etc.

如下所示

public class User
{
 
    [Display(Name = "UserID", ResourceType = typeof(Resource))]
    public int UserID { getset; }
 
 
    [Display(Name = "UserName", ResourceType = typeof(Resource))]
    [Required(ErrorMessageResourceName = "UserNameRequired", ErrorMessageResourceType = typeof(Resource))]
    [RegularExpression(@"^[a-zA-Z]+[ a-zA-Z-_]*$", ErrorMessageResourceName = "UserNameIsNotValid", ErrorMessageResourceType = typeof(Resource))]
    public string UserName { getset; }
 
    [Display(Name = "UserMail", ResourceType = typeof(Resource))]
    [Required(ErrorMessageResourceName = "UserMailRequired", ErrorMessageResourceType = typeof(Resource))]
    [EmailAddress(ErrorMessageResourceName = "UserMailIsNotValid", ErrorMessageResourceType = typeof(Resource))]
    public string UserMail { getset; }
 
    [Display(Name = "UserDate", ResourceType = typeof(Resource))]
    [Required(ErrorMessageResourceName = "UserDateRequired", ErrorMessageResourceType = typeof(Resource))]
    [DataType(DataType.Date, ErrorMessageResourceName = "UserDateIsNotValid", ErrorMessageResourceType = typeof(Resource))]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy hh:mm:ss}", ApplyFormatInEditMode = true)]
    public DateTime UserDate { getset; }
 
 
}

我有一个用户DTO

类似于以下类似的属性用户

Like below similar properties as User

public class UserDto
{
 
 
    [Display(Name = "UserID", ResourceType = typeof(Resource))]
    public int UserID { getset; }
 
    [Display(Name = "UserName", ResourceType = typeof(Resource))]
    [Required(ErrorMessageResourceName = "UserNameRequired", ErrorMessageResourceType = typeof(Resource))]
    [RegularExpression(@"^[a-zA-Z]+[ a-zA-Z-_]*$", ErrorMessageResourceName = "UserNameIsNotValid", ErrorMessageResourceType = typeof(Resource))]
    public string UserName { getset; }
 
    [Display(Name = "UserMail", ResourceType = typeof(Resource))]
    [Required(ErrorMessageResourceName = "UserMailRequired", ErrorMessageResourceType = typeof(Resource))]
    [EmailAddress(ErrorMessageResourceName = "UserMailIsNotValid", ErrorMessageResourceType = typeof(Resource))]
    public string UserMail { getset; }
 
    [Display(Name = "UserDate", ResourceType = typeof(Resource))]
    [Required(ErrorMessageResourceName = "UserDateRequired", ErrorMessageResourceType = typeof(Resource))]
    [DataType(DataType.DateTime, ErrorMessageResourceName = "UserDateIsNotValid", ErrorMessageResourceType = typeof(Resource))]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy hh:mm:ss}", ApplyFormatInEditMode = true)]
    public DateTime UserDate { getset; }
 
}

我有一个 视图模型 如下...

I have a  ViewModel  like below...

public class UserVm
{
    public UserVm()
    {
        this.UserDtos = new HashSet<UserDto>();
    }
    public UserDto UserDto { getset; }
 
    public IEnumerable<UserDto> UserDtos { getset; }
 
}

我有一个如下所示的视图...

I have a View Like below...

@model aa.Models.UserVm
@{
    ViewBag.Title = "Index";
}
 
<h2>Index</h2>
 
@if (Model != null)
{
 
    using (Html.BeginForm("Index""Home"FormMethod.Post))
    {
        @Html.Partial("~/Views/Home/UserDtoPartial.cshtml", Model.UserDto);
    }
 
    <hr />
    <div style="background-color:#df8941">
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th>
                        @Html.LabelFor(m => m.UserDto.UserID)
                    </th>
                    <th>
                        @Html.LabelFor(m => m.UserDto.UserName)
                    </th>
                    <th>
                        @Html.LabelFor(m => m.UserDto.UserMail)
                    </th>
                    <th>
                        @*@Html.LabelFor(m => m.UserDto.UserDate)*@
                    </th>
                    <th>
                        @aa.Resources.Resource.Edit
                    </th>
                    <th>
                        @aa.Resources.Resource.Delete
                    </th>
                </tr>
            </thead>
            <tbody>
                @Html.Partial("UserDtoPartialDisplay", Model.UserDtos)
 
            </tbody>
 
        </table>
 
    </div>
 
 
}

我用于提交的部分

@model aa.Models.UserDto
  <div style="background-color:sandybrown;padding:10px;">
        <div class="form-group">
            @Html.TextBoxFor(m => Model.UserName, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => Model.UserName)
        </div>
        <div class="form-group">
            @Html.TextBoxFor(m => Model.UserMail, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => Model.UserMail)
        </div>
        @*<div class="form-group">
            @Html.TextBoxFor(m => Model.UserDate,Model.UserDate.ToString("dd-MM-yyyy"), new { @class = "form-control" })
            @Html.ValidationMessageFor(m => Model.UserDate)
        </div>*@
        <div class="form-group">
            @Html.HiddenFor(m => Model.UserID)
            <input type="submit" value="@aa.Resources.Resource.Submit" class="btn btn-info" />
        </div>
    </div>

我在视图中使用的用户列表的部分 

the partial for the list of user I use on view to display

@model  IEnumerable<aa.Models.UserDto>
@foreach (var item in Model)
{
    <tr>
        <td>
            @item.UserID
        </td>
        <td>
            @item.UserName
        </td>
        <td>
            @item.UserMail
        </td>
        <td>
            @*@item.UserDate*@
        </td>
        <td>
            @Html.ActionLink(aa.Resources.Resource.Edit, "Index""Home"new { id = item.UserID }, new { @class = "btn btn-info" })
        </td>
        <td>
            @Html.ActionLink(aa.Resources.Resource.Delete, "DeleteUser""Home"new { id = item.UserID }, new { @class = "btn btn-info" })
        </td>
    </tr>
}
 

The HttpPost Action which response the request

The HttpPost Action which response the request

is

Like below..

Like below..

       [HttpPost]
       public ActionResult Index(UserDto userDtox)
       {

           if (ModelState.IsValid)
           {
               User usr = ctx.Userlar.Find(userDtox.UserID);
               Mapper.Map(userDtox, usr);
               this.ctx.SaveChanges();
           }
           List<UserDto> usersDtos = Mapper.Map<List<UserDto>>(ctx.Userlar.ToList<User>());
           UserVm usrVm = new UserVm
           {
               UserDto = userDtox,
               UserDtos = usersDtos
           };
           return View(usrVm);
       }

the problem is it does not validate allays.

the problem is it does not validate allays.

I need to clear cash I need to add some codes before modelstate.isvalid.

I need to clear cash I need to add some codes before modelstate.isvalid.

like belove

like belove

if(userDtox.UserName==null)
           {
               ModelState.AddModelError("UserNameRequired",Resources.Resource.UserNameRequired);
           

I need to force to check

I need to force to check

Than it works..

Than it works..

is it not a bit strange....)???

is it not a bit strange....)???

推荐答案

MVC is primarily used for Web applications, and you got some html code in there.

MVC is primarily used for Web applications, and you got some html code in there.

So I asume this is a ASP.Net web application. ASP.Net has it’s own Forum that is also used for everything web related:

So I asume this is a ASP.Net web application. ASP.Net has it's own Forum that is also used for everything web related:

http://forums.asp.net/


这篇关于MVC嵌套对象验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆