asp.net-mvc – .input-validation-error为表单重新显示失败值时的文本框

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – .input-validation-error为表单重新显示失败值时的文本框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经要求 question知道为什么在我的应用程序中,当我使用模型绑定来验证模型(TryUpdateModel())时,文本框被突出显示(即红色边框和粉红色阴影背景应用于文本框)但是当我使用时手动验证(ModelState.AddModelError).现在已经2天了,没有任何答案.我自己尝试过所有事情而没有成功.所以,我决定以不同的方式提出这个问题.

我理解IT的方式,这就是ModelBinding如何处理请求.

> ModelBinding从httpcontext获取传入值
>它实例化该模型的对象
>尝试将这些值解析为对象
>如果某个属性出现问题,它会使用ModelState.AddModelError标记出现错误属性.
>重新显示视图

这是我的问题当重新显示表单时:

正在为其值无效突出显示的文本框做了什么?

我知道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 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>
<%} %>

谢谢你的帮助.

解决方法

实际上,这个突出显示是由呈现文本框的html助手执行的.它使用给定的密钥检查模型状态是否存在错误,并在必要时添加必要的CSS类.
原文链接:https://www.f2er.com/aspnet/248699.html

猜你在找的asp.Net相关文章