2010-08-13 11 views
8

के लिए पुन: प्रदर्शित कर रहा है मैं एक question पूछा है पता करने के लिए क्यों, अपने आवेदन में, बक्सें पर प्रकाश डाला जा रहा है (यानी लाल बॉर्डर और गुलाबी-धूसरित backgroung पर लागू होते हैं टेक्स्टबॉक्स) जब मैं मॉडल को सत्यापित करने के लिए मॉडलबाइंडिंग का उपयोग करता हूं (TryUpdateModel()) लेकिन जब मैं मैन्युअल रूप से मान्य करता हूं (ModelState.AddModelError)। बिना किसी जवाब के 2 दिन हो गए हैं। मैंने बिना किसी सफलता के हर चीज की कोशिश की है। तो, मैं सवाल अलग से पूछने का फैसला करता हूं।.input-मान्यता त्रुटि जब विधि विफल हो गई मूल्य

तरह से मैं यह समझ है, तो यहां ModelBinding एक अनुरोध व्यवहार करता है।

  1. ModelBinding
  2. यह है कि मॉडल की एक वस्तु का दृष्टांत
  3. प्रयास वस्तु
  4. को उन मूल्यों को पार्स करने के लिए अगर वहाँ है someting एक संपत्ति के साथ गलत है, यह करने के लिए ModelState.AddModelError का उपयोग करता HttpContext से आने वाली मूल्यों को प्राप्त उन गुणों को चिह्नित करें जिनमें उनके साथ कुछ गड़बड़ है।
  5. पुन: प्रदर्शित करता है दृश्य

यहाँ मेरे सवाल जब विधि को पुनः प्रदर्शित किया जाता है है:

क्या बक्सें जिनके मान पर प्रकाश डाला करने के लिए मान्य नहीं हैं के लिए किया जा रहा है?

मुझे पता है कि इस तरह के रूप Site.css में कुछ कक्षाएं, है कि .input-मान्यता त्रुटि और .field-मान्यता त्रुटि कि पाठ बॉक्स के लिए लागू किया जाता है। हो सकता है कि ModelBinding आंतरिक रूप से इस तरह के AddCss के रूप में ("# MyTextBox", ".input-मान्यता त्रुटि") एक आदेश का उपयोग करता है।

अगर मुझे पता है कि यह कैसे काम करता है, तो मैं इसे (शायद) मैन्युअल रूप से लागू कर सकता हूं और अपनी समस्या का समाधान कर सकता हूं।

संपादित

@Ian गैलोवे द्वारा अनुरोध के रूप में, यहाँ कोड है

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

मैं दृश्य में से एक choosed है (उन्हें अनुभव के सभी के रूप में एक ही मुद्दा)। दृश्य 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> 
<%} %> 

मदद के लिए धन्यवाद कहा जाता है।

+0

आप पाठ बॉक्स के लिए दृश्य कोड, और ग # कोड जहां आप मैन्युअल मॉडल त्रुटि कृपया जोड़ रहे हैं पोस्ट कर सकते हैं? –

+0

@ इयान गैलोवे: – Richard77

+0

@Ian Galloway से ऊपर संपादित करें: मैं कोड पोस्ट करने का प्रयास करता हूं लेकिन फॉर्मेटिंग अच्छा नहीं है। इसलिए मैं कुछ कोड चुनने और उन्हें सब कुछ कॉपी करने के बजाय हाथ से लिखने की कोशिश करूंगा। – Richard77

उत्तर

5

दरअसल को हाइलाइट करने वाले HTML मददकर्ताओं द्वारा प्रदर्शित किया जाता है जो टेक्स्टबॉक्स प्रस्तुत करते हैं। यह जांचता है कि दिए गए कुंजी के साथ मॉडल स्थिति में कोई त्रुटि है और यदि आवश्यक हो तो आवश्यक सीएसएस कक्षाएं जोड़ती हैं।

+0

हैलो @ डारिन डिमिट्रोव। आपको फिर से अच्छा लगा यदि यह हाइलाइटिंग एचटीएमएल सहायक द्वारा किया जाता है, तो जब मैं TryUpdateModel के बजाय ModelState.AddModelError का उपयोग करता हूं, तो यह सहायक त्रुटि का पता नहीं लगा रहा है? – Richard77

+0

यदि आप 'TryUpdateModel' का उपयोग करते हैं तो क्या दिए गए कुंजी के साथ ModelState शब्दकोश में कोई त्रुटि जोड़ा गया है? यदि नहीं तो संभवतः यही कारण है कि एचटीएमएल हेल्पर्स इसे नहीं उठाते हैं। –

+0

हां। वास्तव में, TryUpdateModel और ModelState दोनों। AddModelError ModelSate में त्रुटियां जोड़ें। और, दोनों मामलों में, Html.ValidateSummary() फ़ॉर्म के शीर्ष पर त्रुटि संदेश प्रदर्शित करता है। हालांकि, Html.validateMessageFor() केवल तभी काम करता है जब मैं TryUpdateModel का उपयोग करता हूं, मॉडलस्टेट के साथ नहीं। AddModelError()। वैसे ही, टेक्स्टबॉक्स केवल TryUpdateModel के साथ हाइलाइट किए जाते हैं, मॉडलस्टेट के साथ नहीं। AddModelError()। – Richard77

4

मुझे संदेह है कि आपके पास जो समस्या है वह है कि "कुंजी" पैरामीटर जिसे आप ModelState में पास करते हैं। AddModelError को फ़ॉर्म पर मिलान नियंत्रण के नाम से मेल खाना चाहिए।

(अन्यथा, आप कैसे उम्मीद करेंगे दृश्य इंजन को पता है जो एक त्रुटि राज्य में प्रस्तुत करने के लिए नियंत्रण?)

+0

@ इयान गैलोवे: मैंने संदेह करना शुरू कर दिया है कि मैंने सही ढंग से कुंजी नहीं लिखा है। फिर दूसरा संपादन देखें। – Richard77

+0

एचएम। क्या आप अपने मार्कअप में एक नज़र डाल सकते हैं और मुझे बता सकते हैं कि Html.TextBoxFor (x => x.registrationData.Objective) का नाम क्या है? –

+0

<इनपुट आईडी = "पंजीकरणडेटा_ऑब्जेक्टिव" नाम = "पंजीकरणडेटा। ऑब्जेक्टिव" प्रकार = "टेक्स्ट" मान = "" /> – Richard77

0

हाइलाइटिंग पाठ बॉक्स पर लागू होता है जो सीएसएस के द्वारा किया जा रहा है। तो आप नीचे दिए गए सीएसएस जोड़ने की जरूरत:

.input-validation-error { 
border: 1px solid #ff0000; 
background-color: #ffeeee; 

}

संबंधित मुद्दे