This Question benzerdir, ancak kabul edilen yanıt sunucu tarafını çözer, istemci tarafı çözümleriyle ilgilenirim. ViewBag.SomeChoice seçimlerDropDownListFor Kesintisiz Doğrulama Gerekiyor Doğru öznitelikler alınmıyor
seçkin bir listesini içeren bu ViewModel
public class MyViewModel
{
public string ID { get; set; }
[Required(ErrorMessage = "I DEMAND YOU MAKE A CHOICE!")]
[Display(Name = "Some Choice")]
public int SomeChoice{ get; set; }
[Required(ErrorMessage = "I DEMAND YOU MAKE A CHOICE!")]
[Display(Name = "Keyword")]
public string Keyword { get; set; }
}
ve jilet
<div>
@Html.LabelFor(model => model.SomeChoice, new { @class = "label" })
@Html.DropDownListFor(model => model.SomeChoice, (SelectList)ViewBag.SomeChoice, "Select...")
@Html.ValidationMessageFor(model => model.SomeChoice)
</div>
Verilen ve farz
render html veri val almaz = "true" data-val-required = "BİR SEÇİM YAPABİLİRSİNİZ!" # Html.EditorFor (model => model.Keyword) veya @ Html.TextBoxFor gibi öznitelikler oluşturulur.
NEDEN?
bir class = göndermek jQuery Validation sınıf anlambilimini ve blokları kullanır ama mesajı göstermez bu yüzden
@Html.DropDownListFor(model => model.SomeChoice, (SelectList)ViewBag.SomeChoice, "Select...", new { @class = "required" })
gibi kendisine "Gerekli" ekleyerek. Buna hakkım koyacağız
@Html.DropDownListFor(model => model.SomeChoice, (SelectList)ViewBag.SomeChoice, "Select...", new Dictionary<string, object> { { "data-val", "true" }, { "data-val-required", "I DEMAND YOU MAKE A CHOICE!" } })
orada niteliklerini ve bloklar göndermek ve mesajı gösterir ama benim ViewModel
üzerine ben RequiredAttribute errorMessage yararlanmak gelmez bu tür şeyleri yapabilir Bu yüzden, bir diğer dehşet Verme Uzmanı gibi davranan bir DropDownListFor yazan var mı? İşte
DÜZENLEME
@using Arc.Portal.Web.Host.Controllers
@model MyViewModel
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(model => model.SomeChoice)
@Html.DropDownListFor(model => model.SomeChoice, (SelectList)ViewBag.SomeChoice, "Select...")
@Html.ValidationMessageFor(model => model.SomeChoice)
</div>
<button type="submit">OK</button>
}
About.cshtml
HomeController.cspublic class MyViewModel
{
[Required(ErrorMessage = "I DEMAND YOU MAKE A CHOICE!")]
[Display(Name = "Some Choice")]
public int? SomeChoice { get; set; }
}
public ActionResult About()
{
var items = new[] { new SelectListItem { Text = "A", Value = "1" }, new SelectListItem { Text = "B", Value = "2" }, new SelectListItem { Text = "C", Value = "3" }, };
ViewBag.SomeChoice = new SelectList(items,"Value", "Text");
ViewData.Model = new MyViewModel {};
return View();
}
yılında benim TAM kod
Ve burada işlenen kodu
olduğunu Ayrıca[Required(ErrorMessage = "I DEMAND YOU MAKE A CHOICE!")]
[Display(Name = "Some Choice")]
public int? SomeChoice { get; set; }
yılında:
Geri benim kontrolöre nakleder ... Bu
Int nullable yaptım ve hala denetimde veri doğrulama öğeleri yazmak için başarısız oluyor – Peter