.input-validation-error в текстовое поле, когда форма повторно отображается для ошибочного значения

Я задал вопрос , чтобы узнать, почему в в моем приложении текстовые поля выделяются (т.е. к текстовому полю применяются красная граница и фон с розовой заливкой), когда я использую привязку модели для проверки модели ( TryUpdateModel () ), но не когда я проверяю вручную ( ModelState.AddModelError ). Прошло 2 дня без ответа. Я все пробовал сам безуспешно. Итак, я решаю задать вопрос по-другому.

Насколько я понимаю, вот как ModelBinding обрабатывает запрос.

  1. ModelBinding получает входящие значения из httpcontext
  2. Он создает экземпляр объекта этой модели
  3. Пытается преобразовать эти значения в объект
  4. Если что-то не так со свойством, он использует ModelState.AddModelError, чтобы отметить свойства, с которыми что-то не так.
  5. Повторно отображает представление

Вот мой вопрос Когда форма отображается повторно:

Что делается для текстовых полей, значения которых недопустимы для выделения?

Я знаю, что в Site.css, например .input-validation-error и .field-validation-error , которые применяются к текстовому полю. Возможно, ModelBinding использует внутри себя такую ​​команду, как AddCss ("# MyTextBox", ".input-validation-error").

Если я знаю, как это работает, я могу (возможно) применить это вручную и решить свою проблему.

РЕДАКТИРОВАТЬ

По просьбе @Ian Galloway, вот код

public class RegistrationController : Controller
{
  public FormViewModel formViewModel;
  private RegistrationService _registrationService = new RegistrationService();
  private SaveService _saveService = new SaveService();
  protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var serialized = Request.Form["formViewModel"];
        if (serialized != null)
        {
            formViewModel = (FormViewModel)new MvcSerializer()
                            .Deserialize(serialized);
            TryUpdateModel(formViewModel);
        }
        else
            formViewModel = (FormViewModel)TempData["formViewModel"] 
                            ?? new   FormViewModel();
    }
  protected override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        if (filterContext.Result is RedirectToRouteResult)
            TempData["formViewModel"] = formViewModel;
    }
    public ActionResult SetUpOrganization(string cancel, string nextButton)
    {
        if ((nextButton != null) && ModelState.IsValid)
        {
            if (formViewModel.navigationData.IsAReview)
                return RedirectToAction("RequestPreview");
            return RedirectToAction("ChooseTypeOrganization");
        }
        ViewData["Cities"] = _registrationService.Get_Cities();
        formViewModel.navigationData.NextAction = "SetUpOrganization";
        return View(formViewModel);
    }
  public ActionResult ChooseTypeOrganization(string backButton, string nextButton)
    {
        if (backButton != null)
        {
            return RedirectToAction("SetUpOrganization");
        }
        if (nextButton != null)
        {
            if (formViewModel.navigationData.IsAReview)
                return RedirectToAction("RequestPreview");
            return RedirectToAction("DocumentsPresented");
        }
        ViewData["TypeOrganization"] = _registrationService.Get_AllTypeOrganization();
        formViewModel.navigationData.NextAction = "ChooseTypeOrganization";
        return View(formViewModel);
    }
    public ActionResult DocumentsPresented(string backButton, string nextButton)
    {
        if (backButton != null)
        {
            return RedirectToAction("ChooseTypeOrganization");
        }
        if (nextButton != null)
        {
            //Validation
            if (string.IsNullOrEmpty(formViewModel.registrationData.DocumentPresente))
            {
                ModelState.AddModelError("DocumentPresente", "Veuillez préciser votre
                  autorisation");
                return View(formViewModel);
            }
            //Navigation
            if (formViewModel.navigationData.IsAReview)
                return RedirectToAction("RequestPreview");
            return RedirectToAction("PeopleRecommended");
        }
        formViewModel.navigationData.NextAction = "DocumentsPresented";
        return View(formViewModel);
    }
  public ActionResult PeopleRecommended(string backButton, string nextButton, string deleteButton,
                                          string deletePerson, string addPerson)
    {
        if (backButton != null)
        {
            return RedirectToAction("DocumentsPresented");
        }
        if (nextButton != null)
        {
            ModelState.Clear();
            if (formViewModel.registrationData.PeopleRecommended.Count == 0)
                ModelState.AddModelError("", "Il faut absolument designer un responsable pour la requête");
            //
            if (ModelState.IsValid)
            {
                if (formViewModel.navigationData.IsAReview)
                    return RedirectToAction("RequestPreview");
                return RedirectToAction("StateObjectifs");
            }
            else
            {
                return View(formViewModel);
            }
        }
        //
        if (addPerson != null)
        {
            if (ModelState.IsValid)
            {
                formViewModel.registrationData.PeopleRecommended.Add(
                    _registrationService.Translate_PersonToBeAdded_Into_Person(formViewModel.personToBeAdded)
                    );
                formViewModel.personToBeAdded = null;
            }
            else
            {
                formViewModel.navigationData.NextAction = "PeopleRecommended";
                return View(formViewModel);
            }
        }
        if (deleteButton != null)
        {
            formViewModel.registrationData.PeopleRecommended.RemoveAt(int.Parse(deletePerson));
        }
        ViewData.ModelState.Clear();
        formViewModel.navigationData.NextAction = "PeopleRecommended";
        return View(formViewModel);
    }
    [ValidateInput(false)]
    public ActionResult StateObjectifs(string backButton, string nextButton)
    {
        if (backButton != null)
        {
            return RedirectToAction("PeopleRecommended");
        }
        if (nextButton != null)
        {
            if (string.IsNullOrEmpty(formViewModel.registrationData.Objective) ||
               string.IsNullOrEmpty(formViewModel.registrationData.RequestDetails))
            {
                 if (string.IsNullOrEmpty(formViewModel.registrationData.Objective))
                     ModelState.AddModelError("Objective", "Vous devez préciser l'objectif de votre requête");

                 if (string.IsNullOrEmpty(formViewModel.registrationData.RequestDetails))
                     ModelState.AddModelError("RequestDetails", "Vous devez préciser le contenu de votre requête");
                 return View(formViewModel);
            }

            if (formViewModel.navigationData.IsAReview)
                return RedirectToAction("RequestPreview");
            return RedirectToAction("StateDeadLine");
        }
        return View(formViewModel);
    }

    public ActionResult StateDeadLine(string backButton, string nextButton)
    {
        if (backButton != null)
        {
            return RedirectToAction("StateObjectifs");
        }
        if (nextButton != null)
        {
            if (formViewModel.registrationData.ChooseDifferentDeadLine)
            {
                if (formViewModel.registrationData.DifferentDeadline == null ||
                    string.IsNullOrEmpty(formViewModel.registrationData.ReasonsForDifferentDeadLine))
                {
                    if (formViewModel.registrationData.DifferentDeadline == null)
                        ModelState.AddModelError("DifferentDeadline", "Clickez pour choisir une nouvelle date");
                    if (string.IsNullOrEmpty(formViewModel.registrationData.ReasonsForDifferentDeadLine))
                        ModelState.AddModelError("ReasonsForDifferentDeadLine", "Expliquez brievement pour quoi ce changement");

                    return View(formViewModel);
                }
            }
            return RedirectToAction("RequestPreview");
        }
        formViewModel.navigationData.NextAction = "StateDeadLine";
        return View(formViewModel);
    }
  public ActionResult RequestPreview(string backButton, string nextButton, string reviewInput, string save)
    {
        if (backButton != null)
        {
            return RedirectToAction("StateDeadLine");
        }
        if (nextButton != null)
        {
            _saveService.Save_FormViewModel(formViewModel);   
            return RedirectToAction("Index", "Home");
        }
        if (reviewInput != null)
        {
            formViewModel.navigationData.IsAReview = true;
            return RedirectToAction(RedirectHelpers.RedirectReviewAction(formViewModel.navigationData, reviewInput));
        }
        ViewData["TypeOrganization_Summary"] = _registrationService.Get_TypeOrganization_Summary(
                                                                formViewModel.registrationData.TypeOrganizationID                                                                   );
        if (save != null)
        {
            _saveService.Save_FormViewModel(formViewModel);
            return RedirectToAction("Index", "Home");
        }

        formViewModel.navigationData.NextAction = "RequestPreview";
        return View(formViewModel);
    }
}

РЕДАКТИРОВАТЬ номер 2

Я выбрал одно из представлений (поскольку все они сталкиваются с одной и той же проблемой). Вид называется StateObjectifs.aspx

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<!-- Script Begin -->
<script src="../../Scripts/tiny_mce/tiny_mce.js" type="text/javascript"></script>
<script type = "text/javascript">
    tinyMCE.init({
        mode: "textareas",
        theme: "advanced",

        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location: "bottom",
        theme_advanced_resizing: true
    });
</script>
 <!-- End Script -->
<fieldset id = "StateObjectifs">
    <legend>Etape 5:</legend>
    <div id = "left-pane" style = "width:700px">
    <%using (Html.BeginForm("StateObjectifs", "Registration"))
  { %>
   <%: Html.ValidationSummary() %>
  <% = Html.Serialize("formViewModel", Model) %> 
  <table>
    <tr>
        <th><% = Html.LabelFor(x=>x.registrationData.Objective) %></th>
        <td><% = Html.TextBoxFor(x=>x.registrationData.Objective) %>
            <%: Html.ValidationMessageFor(x =>x.registrationData.Objective, "*")%></td>
    </tr>
    <tr>
        <th colspan = "2"><% = Html.LabelFor(x =>x.registrationData.RequestDetails) %></th>
    </tr>
    <tr>
        <td colspan = "2">
            <% = Html.TextAreaFor(x =>x.registrationData.RequestDetails, 10, 75, null) %>

        </td>
    </tr>
  </table>
  </div>
  <div id = "right-pane">
        <ul>
            <li>Les cases pour lesquelles le titre se termine par (*) sont obligatoires</li>
            <li>Pour mieux vous servir, veuillez décrire clairement de manière concise
                votre requête. Par exemple: <i>Les données de l'USAID sur le secteur santé pour l'année 2009</i></li>
        </ul>  
      </div>
      <div class = "clear"></div>

  <fieldset>
            <% Html.RenderPartial("NavigationView", Model.navigationData); %>
  </fieldset>
</fieldset>
<%} %>

Спасибо за помощь.

8
задан Community 23 May 2017 в 10:28
поделиться

2 ответа

Фактически это выделение выполняется помощниками HTML, которые визуализируют текстовые поля. Он проверяет, есть ли ошибка в состоянии модели с данным ключом, и при необходимости добавляет необходимые классы CSS.

5
ответ дан 5 December 2019 в 17:32
поделиться

Я подозреваю, что проблема в том, что "ключевой" параметр, который вы передаете в ModelState.AddModelError, должен совпадать с именем соответствующего элемента управления в форме.

(В противном случае, как вы ожидаете, что движок представления узнает, какой элемент управления визуализировать в состоянии ошибки?)

4
ответ дан 5 December 2019 в 17:32
поделиться
Другие вопросы по тегам:

Похожие вопросы: