英文:
Check if datetime existing in database mvc 5
问题
以下是您要翻译的内容:
我正在使用MVC 5,我想进行验证以检查数据库中是否存在Event的DateOfEvent,并且我正在使用远程验证,但它不起作用。我希望看到类似于这样的输出
点击这里查看图片
我正在使用SQL Server,这是我的数据库
点击这里查看图片
模型
public partial class tblEvent
{
public int EventId { get; set; }
public string Descripstions { get; set; }
[Display(Name = "DateOfEvent")]
[DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy hh:mm tt}", ApplyFormatInEditMode = true)]
//使用远程验证属性
[Remote("IsDateOfEventAlreadySigned", "Event", HttpMethod = "POST", ErrorMessage = "Date Of Event already exists in database.")]
public DateTime? DateOfEvent { get; set; }
[Display(Name = "EmpName")]
[Remote("IsEmpNameAlreadySigned", "Event", HttpMethod = "POST", ErrorMessage = "EmpName already exists in database.")]
public string EmpName { get; set; }
}
控制器
public class EventController : Controller
{
[HttpPost]
public JsonResult IsDateOfEventAlreadySigned(DateTime? DateOfEvent)
{
return Json(db.tblEvents.Any(u => u.DateOfEvent == DateOfEvent), JsonRequestBehavior.AllowGet);
}
[HttpPost]
public JsonResult IsEmpNameAlreadySigned(string EmpName)
{
return Json(!db.tblEvents.Any(u => u.EmpName == EmpName), JsonRequestBehavior.AllowGet);
}
private MyDatabaseEntities db = new MyDatabaseEntities();
}
英文:
I'm using MVC 5 and I want to make validation to check if DateOfEvent existing in database and I'm using remote validation but it's not working. I want to see like this output
enter image description here
I'm using sql server and this is my database
enter image description here
Model
public partial class tblEvent
{
public int EventId { get; set; }
public string Descripstions { get; set; }
[Display(Name = "DateOfEvent")]
[DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy hh:mm tt}", ApplyFormatInEditMode = true)]
//Using Remote validation attribute
[Remote("IsDateOfEventAlreadySigned", "Event", HttpMethod = "POST", ErrorMessage = "Date Of Event already exists in database.")]
public DateTime? DateOfEvent { get; set; }
[Display(Name = "EmpName")]
[Remote("IsEmpNameAlreadySigned", "Event", HttpMethod = "POST", ErrorMessage = "EmpName already exists in database.")]
public string EmpName { get; set; }
}
Controller
public class EventController : Controller
{
[HttpPost]
public JsonResult IsDateOfEventAlreadySigned(DateTime? DateOfEvent)
{
return Json(db.tblEvents.Any(u => u.DateOfEvent == DateOfEvent), JsonRequestBehavior.AllowGet);
}
[HttpPost]
public JsonResult IsEmpNameAlreadySigned(string EmpName)
{
return Json(!db.tblEvents.Any(u => u.EmpName == EmpName), JsonRequestBehavior.AllowGet);
}
private MyDatabaseEntities db = new MyDatabaseEntities();
答案1
得分: 0
只需将js包含到您的.cshtml文件中
我希望这能解决您的问题
有关更多详细信息,请参阅此URL:
https://www.c-sharpcorner.com/blogs/remote-validation-in-mvc-5-using-remote-attribute
英文:
Just include js to your .cshtml
I hope this will fix your issue
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
and for more details refer this URL
https://www.c-sharpcorner.com/blogs/remote-validation-in-mvc-5-using-remote-attribute
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论