简体   繁体   English

mvc [Remote]标签参考单独的项目控制器

[英]mvc [Remote] tag reference separate project controller

first let me say I think I know my issue, but I don't know how to solve it. 首先让我说我想我知道我的问题,但是我不知道如何解决。 I've been tasked with cleaning up the validation section for our application. 我的任务是清理应用程序的验证部分。 We have entity models and MVC Application in separate projects, one called "core" and the other called "webapp" 我们在单独的项目中有实体模型和MVC应用程序,一个称为“核心”,另一个称为“ webapp”

I can't seem to get my [Remote] validation to fire. 我似乎无法启动[远程]验证。 I've done my research, but I believe it's because, I'm trying to reference a controller in my separate project. 我已经完成了研究,但是我相信这是因为,我试图在我的单独项目中引用控制器。

Simplified model in my core project looks something like 我核心项目中的简化模型看起来像

namespace Project.Core.Models
{
    [Table(name: "MT_DATABASE_SERVERS")]
    public class DatabaseServer : ModelBase
    {
        [Key]
        public int Id { get; set; }

        [Required]
        [MaxLength(500)]
        [Display(Name = "Database Server")]
        [Remote("IsPropertyExists","DatabaseServers", HttpMethod = "POST",ErrorMessage = "This Server is already in use.")]
        public string ServerName { get; set; }
    }
}

and here is a simplified controller logic 这是简化的控制器逻辑

namespace Project.WebApp.Controllers.Servers
{
    public class DatabaseServersController : ControllerBase
    {
        [HttpPost]
        public ActionResult Create(DatabaseServer databaseServer
        {
            using (var db = GetMTContext()) 
            {
                db.DatabaseServers.Add(databaseServer);
                db.SaveChanges();
                return RedirectToAction("List");
            }
        }

        [HttpPost]
        public ActionResult IsPropertyExists(string property)
        {
            using (var db = GetMTContext())
            {
                return Json(!db.DatabaseServers.Any(x => x.ServerName == property), JsonRequestBehavior.AllowGet);
            }
        }
    }
}

you need to specify AdditionalFields = "property" then your code like 您需要指定AdditionalFields = "property"然后将您的代码指定AdditionalFields = "property"

 [Remote("IsPropertyExists","DatabaseServers", AdditionalFields = "property", HttpMethod = "POST",ErrorMessage = "This Server is already in use.")]
    public string ServerName { get; set;a  }

But the condition is you need to add or exist additional field property in your model or entity. 但是条件是您需要在模型或实体中添加或存在其他字段property

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM