我試圖實現簡單的CMS。我有Page
類的實例,我想將其添加到數據庫並編輯它們。 Page
中的UrlName
應該是唯一的,所以我創建了自定義驗證器,但是,在編輯時,如果不將UrlName
更改爲未出現的格式,我無法提交表單。我如何將額外的數據傳遞給我的VerifyUrl
或者有另一種方法可以解決這個問題?ASP.NET MVC應用程序中的不同驗證操作
public class Page : IUpdatable<Page>
{
public int ID { get; set; }
[Required]
[DataType(DataType.Url)]
[Remote(action: "VerifyUrl", controller: "Pages")]
public string UrlName { get; set; }
}
這是PagesController
public JsonResult VerifyUrl(string UrlName)
{
if (!db.Pages.Any(x => x.UrlName.Equals(UrlName)))
return Json(data: true);
return Json(data: "This Url is already in use");
}
我的驗證方法,而這在我看來是
<input asp-for="UrlName" class="form-control" />
<span asp-validation-for="UrlName" class="text-danger" />
請不要將[標籤:asp.net-mvc]用於ASP.NET Core,請使用[標籤:asp.net-core-mvc] – Tseng