我可以跳过ASP MVC 5(C#)中要由ModelState.IsValid()验证的属性/字段吗?或为“隐藏字段”分配值?

时间:2019-07-11 07:42:44

标签: c# asp.net-mvc-5 entity-framework-6

我正在基于ASP.Net MVC 5(C#)创建一个Web应用程序,并且停留在特定类型的验证上。基本上,我有5个模型,它们的对象包含在视图模型中。这五个模型之一作为视图模型的列表包括在内。由于这些模型之间存在关系,因此实现了外键,我将外键注释为必需,因为它们必须存在。

场景 型号类别: 客户-应用中的客户 AddressOf-属性和客户端的地址 ClientPhone-客户电话-具有客户外键 ClientEmail-客户的电子邮件-具有客户的外键 顾问-在管理系统中处理客户的顾问

现在,我在View.cshtml中为客户端对象,地址对象,ClientPhone,ClientEmail和下拉列表列出了顾问字段。不包括ClientPhone.ClientCNIC和ClientEmail.ClientCNIC的字段,这些字段是外键,当我在“客户表”中输入数据时将在控制器中输入这些字段。 ClientCNIC确实在View.cshtml中具有字段,但其来自客户端表。由于ClientPhone和ClientEmail中的ClientCNIC字段(外键)具有注释[Required],因此显示验证错误。

我想要的是,将为Client.ClientCNIC创建的字段的值分配给属性ClientPhone.ClientCNIC和ClientEmail.ClientCNIC。

谢谢!

控制器

[HttpPost]
        public ActionResult Save(ClientFormViewModel clientFormViewModel)
        {
            if (ModelState.IsValid)
            {
                ViewBag.error = "";

                var Client = clientFormViewModel.Client;
                var Email = clientFormViewModel.ClientEmail;
                var Address = clientFormViewModel.AddressOf;
                var phone = clientFormViewModel.ClientPhone;

                _context.addressof.Add(Address);
                _context.SaveChanges();
                Client.addressofId = _context.addressof.Select(e => e.addressofId).Max();
                _context.Clients.Add(Client);
                try
                {
                    _context.SaveChanges();
                }
                catch (DbUpdateException)
                {
                    _context.addressof.Remove(Client.AddressOf);
                    _context.SaveChanges();
                    ViewBag.Duplicate = "Duplicate Client";
                    clientFormViewModel.Consultants = _context.Consultants.ToList();
                    return View("ClientForm", clientFormViewModel);
                }

                phone.ClientCNIC = Client.ClientCNIC;
                phone.PrimaryPhone = true;
                _context.ClientPhone.Add(phone);

                Email.ClientCNIC = Client.ClientCNIC;
                Email.PrimaryEmail = true;
                _context.clientemail.Add(Email);

                _context.SaveChanges();

                return Content("Successful!");
            }

            ViewBag.error = "Has Error";
            clientFormViewModel.Consultants = _context.Consultants.ToList();
            return View("ClientForm", clientFormViewModel);
        }


        public ActionResult New()
        {
            ViewBag.Type = "New Client";

            var address = new AddressOf();
            var client = new Client();
            var phone = new ClientPhone();
            var email = new ClientEmail();


            var viewModel = new ClientFormViewModel()
            {
                Client = client,
                ClientPhone = phone,
                ClientEmail = email,
                AddressOf = address,
                Consultants = _context.Consultants.ToList()
            };

            viewModel.ClientEmail.ClientCNIC = viewModel.Client.ClientCNIC;
            viewModel.ClientPhone.ClientCNIC = viewModel.Client.ClientCNIC;


            return View("ClientForm", viewModel);
        }

ClientPhone-模型类-与ClientEmail相同的结构

namespace ITCMS4
{
    using System;
    using System.Collections.Generic;

    [Table("ClientPhones")]
    public partial class ClientPhone
    {
        [Key] public int ClientPhoneId { get; set; }
        [Required] public string Phone { get; set; }
        [Required] public bool PrimaryPhone { get; set; }
        [Display(Name = "Client"), ForeignKey("Client")] public string ClientCNIC { get; set; }
        public virtual Client Client { get; set; }
    }
}

1 个答案:

答案 0 :(得分:1)

一种方法是从“违规”属性中删除[Required]属性,同时确保在需要的地方为其分配适当的值。一个ViewModel并不总是必须通过数据注释属性来强制或模仿所有数据模型规则,这就是一个很好的例子。

另一种方法是保留ClientFormViewModel类的属性和create a custom ModelBinder,然后使您将此规则“也分配给该规则”。