Я использую надежную библиотеку для динамической проверки атрибутов.
Модель выглядит следующим образом:
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
всегда ложно.
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);
Я не смог найти проблему, может кто-нибудь помочь мне решить эту проблему?