[英]How to change the ErrorMessage for int model validation in ASP.NET MVC?
[英]Change model before model state validation ASP.NET MVC
我正在使用萬無一失的庫來進行動態屬性驗證。
該模型如下:
public class ProfileQuestion
{
public int QuestionId { get; set; }
public string QuestionText { get; set; }
public int QuestionTypeId { get; set; }
[RequiredIf("Isrequired", true, ErrorMessage = "Required")]
public string Answer { get; set; }
public string Day { get; set; }
public string Month { get; set; }
public string Year { get; set; }
public string Areacode { get; set; }
public string Zipcode { get; set; }
public List<ProfileOptions> OptionsList { get; set; }
public bool Isrequired = false;
}
我們有一個問題列表,其中一些問題是必需的。 如果需要提問,則Isrequired = true
。 我在模型狀態驗證之前設置isrequired
標志,但是ModelState.IsValid
始終為false。
var mandatoryQuestions = objBusLayer.GetMandatoryQuestionIds(m);
foreach (ProfileQuestion question in Response)
{
if (question.QuestionId == 2)
{
string date = question.Year + @"/" + question.Month + @"/" +
question.Day;
DateTime outdate;
if (DateTime.TryParse(date, out outdate))
{
question.Answer = date;
}
}
if (question.QuestionId == 3)
{
question.Answer = question.Areacode + '-' + question.Zipcode;
}
if (mandatoryQuestions.Contains(question.QuestionId.ToString()))
{
question.Isrequired = true;
}
}
if (ModelState.IsValid)
{
Encryption encObj = new Encryption();
string decryptedExtPanelistId = encObj.DecryptText(e, password);
objBusLayer.Register(decryptedExtPanelistId, m, Response);
return RedirectToAction("Register", new { e = e, m = m, pg = pg + 1 });
}
ViewBag.pageId = pg;
ViewBag.extpnltid = e;
ViewBag.mid = m;
return View(Response);
我找不到問題,有人可以幫助我解決此問題嗎?
嘗試結合使用ModelState.Clear()和TryValidateModel()。
ModelState.Clear();
TryValidateModel(Response);
問候。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.