2011-02-03 14 views
9

में किसी पृष्ठ पर टेक्स्ट बॉक्स फ़ील्ड साफ़ करना किसी कारण से मुझे एक साधारण समस्या क्या रोकनी चाहिए। मैं एमवीसी के लिए नया हूं ताकि उसे समझाया जाए।एमवीसी

मैं सबमिट करने के बाद किसी पृष्ठ पर सभी फ़ील्ड साफ़ करने का प्रयास कर रहा हूं।

मैंने नीचे दिए गए कोड को उदाहरणों में उपयोग किया है लेकिन यह नहीं पता कि इसे कहां रखा जाए। मैंने इसे अपने PersonModel.vb में करने की कोशिश की और यह त्रुटियों को फेंकता है। मैंने इसे अपने रिपोजिटरी में आजमाया जहां मैं अपना सबस और फ़ंक्शंस रखता हूं और यह मॉडलस्टेट और ValueProvierResult शब्द (शब्द) पर हर बार त्रुटियों को फेंक देता है।

ModelState.SetModelValue("Key", new ValueProviderResult(null, string.Empty, CultureInfo.InvariantCulture)) 

क्या कोई पृष्ठ पर फ़ील्ड (टेक्स्ट बॉक्स) को साफ़ करने के लिए मेरे लिए कुछ सलाह दे सकता है? मैं एमवीसी के लिए बहुत नया हूं और कुछ सहायक का उपयोग कर सकता हूं।

धन्यवाद

LW

यहाँ कोड है ....

<HttpPost(), MvcReCaptcha.CaptchaValidator()> _ 
Function Nominate(ByVal m As NominationModel, ByVal captchaValid As Boolean) As ActionResult 

    Dim db = New ChampionTrees.Common.DataAccess.ChampionTreesRepository With {.UserName = "SYSTEM"} 
    BindNominateDdls(db) 

    Dim addPost As Boolean = False 
    If (Request.Form("addNominator") <> Nothing) Then 
     m.People.Add(New PersonModel With {.Indicator = PersonIndicator.Nominator}) 
     addPost = True 
    ElseIf Request.Form("addOwner") <> Nothing Then 
     m.People.Add(New PersonModel With {.Indicator = PersonIndicator.Owner}) 
     addPost = True 
    Else 
     For Each f In Request.Form.Keys 
      If f.ToString.StartsWith("deletePerson") Then 

       Dim idx = f.ToString.IndexOf("n") 
       m.People.RemoveAt(Integer.Parse(f.ToString.Substring(idx + 1, f.ToString.Length - (idx + 1)))) 
       addPost = True 
       Exit For 

      End If 
     Next 
    End If 

    If addPost Then 

     For Each v In ModelState.Values.AsEnumerable() 
      v.Errors.Clear() 
     Next 

     Return View(m) 

    End If 

    If m.Tree.Id < 0 AndAlso String.IsNullOrEmpty(m.OtherName) Then 

     Dim err As String = "Either a Common, Scientific, or Other Name must be provided." 
     ModelState.AddModelError("Tree.Id", err) 
     ModelState.AddModelError("OtherName", err) 

    End If 

    If String.IsNullOrEmpty(m.Measurement.CountyForester) = _ 
     String.IsNullOrEmpty(m.Measurement.OtherCountyForester) Then 

     Dim err As String = "A County Forester must be selected or entered (but not both)." 
     ModelState.AddModelError("Measurement.CountyForester", err) 
     ModelState.AddModelError("Measurement.OtherCountyForester", err) 

    End If 

    Dim i As Integer = 0 
    For Each p As PersonModel In m.People 

     If String.IsNullOrEmpty(p.EmailAddress) AndAlso _ 
      (p.Phone.Phone1 Is Nothing Or p.Phone.Phone2 Is Nothing Or p.Phone.Phone3 Is Nothing) Then 

      ModelState.AddModelError(String.Format("People[{0}].Phone", i), "Either an E-mail Address or Phone number must be provided.") 
      ModelState.AddModelError(String.Format("People[{0}].Phone.Phone1", i), "") 
      ModelState.AddModelError(String.Format("People[{0}].Phone.Phone2", i), "") 
      ModelState.AddModelError(String.Format("People[{0}].Phone.Phone3", i), "") 
      ModelState.AddModelError(String.Format("People[{0}].EmailAddress", i), " ") 

     Else 

      Dim int As Integer = 0 
      Dim err As Boolean = False 

      If Not p.Phone.Phone1 Is Nothing AndAlso _ 
          (p.Phone.Phone1.Length <> 3 Or Not Integer.TryParse(p.Phone.Phone1, Int)) Then 

       ModelState.AddModelError(String.Format("People[{0}].Phone.Phone1", i), "") 
       err = True 
      End If 
      If Not p.Phone.Phone2 Is Nothing AndAlso _ 
          (p.Phone.Phone2.Length <> 3 Or Not Integer.TryParse(p.Phone.Phone2, int)) Then 

       ModelState.AddModelError(String.Format("People[{0}].Phone.Phone2", i), "") 
       err = True 
      End If 
      If Not p.Phone.Phone3 Is Nothing AndAlso _ 
       (p.Phone.Phone3.Length <> 4 Or Not Integer.TryParse(p.Phone.Phone3, int)) Then 

       ModelState.AddModelError(String.Format("People[{0}].Phone.Phone3", i), "") 
       err = True 
      End If 

      If err Then ModelState.AddModelError(String.Format("People[{0}].Phone", i), "Phone Number is not numeric.") 

     End If 

     If m.OwnershipType = Ownership.Public AndAlso _ 
      p.Indicator = PersonIndicator.Owner AndAlso _ 
      p.ParcelName Is Nothing Then 
      ModelState.AddModelError(String.Format("People[{0}].ParcelName", i), "The Parcel Name field is required for public nominations.") 
     End If 

     i += 1 

    Next 

    If Not m.UseNominatorsAsOwners AndAlso _ 
     (From e In m.People Where e.Indicator = PersonIndicator.Owner Select e).Count = 0 Then 
     ModelState.AddModelError("UseNominatorsAsOwners", "At least one Owner is required.") 
    End If 

    If Files.Count > 0 AndAlso Not m.ElectronicUseAgreement Then 
     ModelState.AddModelError("ElectronicUseAgreement", "The Electronic Use Agreement must be agreed to.") 
    End If 

    If Not captchaValid Then 
     ModelState.AddModelError("ReCaptcha", "You did not type the verification word correctly. Please try again.") 
    End If 

    If ModelState.IsValid Then 

     ' load our uploads from session 
     For Each f In Files 
      f.Value.Viewable = m.ElectronicUseAgreement 
      m.Uploads.Add(f.Value) 
     Next 

     ' insert the nomination into the db 
     db.InsertNomination(m) 

     ViewData("message") = "Nomination has been submitted" 

    End If 

    ModelState.Clear() 
    Return View(m) 

उत्तर

35

हाय आप उपयोग करने में सक्षम होना चाहिए: ModelState.Clear() और जब आप देखें लौट सभी पिछले दर्ज किया गया डेटा हो जाएगा मंजूरी दे दी।

संपादित करें:

public ActionResult Index() 
{ 
    return View(); 
} 

[HttpPost] 
public ActionResult Index(FormCollection collection) 
{ 
    // This will clear whatever form items have been populated 
    ModelState.Clear(); 

    return View(); 
} 

अद्यतन 2:

अपने कोड में आप ModelState समाशोधन रहे हैं लेकिन अपने गुजर मॉडल (आप इसे मीटर कहा जाता है)

यहाँ कुछ उदाहरण कोड है अपने दृश्य पर वापस जाएं और आपका विचार तब इस मॉडल को चुन रहा है और इसकी गुणों को प्रदर्शित कर रहा है।

उदाहरण के लिए मेरे पास एक पृष्ठ है जिसने पहला नाम और अंतिम नाम स्वीकार किया है और जब मैं पोस्ट करता हूं तो मैं इसे डेटाबेस में जोड़ना चाहता हूं लेकिन फिर उसी दृश्य को वापस कर दूंगा लेकिन मेरे अगले अनुरोध के लिए खाली होगा, मेरा कोड कुछ ऐसा दिखाई देगा:

public ActionResult Index() 
{ 
    return View(); 
} 

[HttpPost] 
public ActionResult Index(Person p) 
{ 
    if (ModelState.IsValid) 
    { 
    // This will clear whatever form items have been populated 
    ModelState.Clear(); 
    // Here I'm just returning the view I dont want a model being passed 
    return View(); 
    } 

    // Here I'm returning the model as there's an error and the user needs to see 
    // what has been entered. 
    return View(p); 
} 
+0

के बाद जब मैं कोशिश है कि यह कहते हैं "मॉडलस्टेट" घोषित नहीं किया गया है। मुझे उस कोड का उपयोग कहां करना चाहिए? – Lee

+0

मैंने इसे एएसपीएक्स पेज पर भी कोशिश की और कहा कि यह मॉडलस्टेट के लिए कोई स्पष्ट() विधि नहीं है ...... – Lee

+0

मैंने कुछ नमूना कोड के साथ अपडेट किया है - अगर आपको अभी भी समझ में नहीं आता है कि आप अपना कोड पोस्ट कर सकते हैं। धन्यवाद – lancscoder

4

यह संभव आप jQuery का उपयोग करें और जैसे

$(document).ready(function(){ 
    $("input").each(function(){ 
     $(this).val(""); 
    }); 
}); 

कुछ करना और शायद लपेट कि किसी प्रकार की है में प्रपत्र उस्तरा कोड तैनात कर सकता है।

+0

मैंने jQuery के बारे में सोचा .... अभी तक कोशिश नहीं की है। मैंने पेज के ऑनलोड ईवेंट में कुछ कोड माना लेकिन यह सुनिश्चित नहीं है कि आप एमवीसी के साथ ऐसा कर सकते हैं। – Lee

3

या अगर ModelState मान्य था तुम सिर्फ वापस पुन: निर्देशित इस तरह कार्रवाई प्राप्त कर सकते हैं:

public ActionResult Index() 
{ 
    return View(); 
} 

[HttpPost] 
public ActionResult Index(Person p) 
{ 
    if (ModelState.IsValid) 
    { 
    // do work and finally back to Get Action 
    return RedirectToAction("Index"); 
    } 

    return View(p); 
} 
+0

'ModelState.Clear()' मेरे लिए काम नहीं किया। लेकिन ऐसा किया। – th1rdey3

+0

धन्यवाद, इसके दृश्य() के बजाय मेरी समस्या RedirectToAction ("अनुक्रमणिका") को हल किया गया है; – adnan

2

Jquery में

$("#btnID").click(function() { 
    $("input[type=text]").val(""); 

}); 

modelstate.clear() सफल प्रस्तुत