2013-07-01 8 views
6

मुझे समझ में नहीं आता क्यों ModelState.isValid मुझे सभी तरीकों से देता है। मैंने ईमेल में कुछ सेट सही किया है और मैं खाली फ़ील्ड चाहता हूं, यह भी सच हो जाता है। मेरा सवाल है, क्षेत्र खाली होने पर मुझे वापस लौटने के लिए क्या करना है और कुछ भी नहीं जब मैंने ईमेल लिखा था?एमवीसी 4. ModelState.IsValid हमेशा सच है

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <div style="padding-top:5px;clear:both;"></div> 
    <% using (Html.BeginForm()) { %> 
     <%: Html.ValidationSummary(true) %> 
     <fieldset> 
       <legend>Email usuario</legend> 

       <div class="editor-field"> 
        <%: Html.TextBoxFor(m => m.Email) %> 
        <%: Html.ValidationMessageFor(m => m.Email) %> 
       </div> 

       <input type="submit" value="Enviar Email" /> 
     </fieldset> 
    <% } %> 
    <div style="padding-top:5px;clear:both;"></div> 
</asp:Content> 

नियंत्रक है:

// 
// GET: /Account/EmailRequest 
public ActionResult EmailRequest() 
{ 
    return View(); 
} 

[HttpPost] 
public ActionResult EmailRequest(string email) 
{ 
    if (ModelState.IsValid) 
    { 
     // save to db, for instance 
     return RedirectToAction("AnotherAction"); 
    } 
    return View(); 
} 

मेरे मॉडल वर्ग है:

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel.DataAnnotations; 
    using System.Globalization; 
    using System.Web.Mvc; 
    using System.Web.Security; 

namespace PortalClient.Models 
{ 
    public class EmailRequest 
    { 

     [Required(ErrorMessage = "required")] 
     public string Email { get; set; } 
    } 
} 
+3

द्वारा यह chek करने की क्षमता है करने के लिए बांधने की मशीन के साथ अपने मॉडल के लिए बाध्य करने की जरूरत है राज्य की जांच करें, आप क्या परिणाम देखते हैं? –

+0

[HttpPost] public ActionResult EmailRequest (EmailRequest ईमेल) { अगर (ModelState.IsValid) { // db से, उदाहरण के वापसी RedirectToAction ("AnotherAction") को बचाने के लिए; } वापसी देखें(); } – Dave

+0

जैसा कि आपने मुझे सुझाव दिया है, मैंने स्ट्रिंग से ईमेलरक्वेट में बदल दिया है और यह शून्य – Dave

उत्तर

6

बदलें string email से अपनी पोस्ट कार्रवाई के हस्ताक्षर

मैं अगले दृश्य फ़ाइल है EmailRequest model पर और फिर राज्य की जांच करें। जैसे

[HttpPost] 
public ActionResult EmailRequest(EmailRequest model) 
{ 
    if (ModelState.IsValid) 
    { 
     // save to db, for instance 
     return RedirectToAction("AnotherAction"); 
    } 
    return View(); 
} 
3

आपको अपने दृश्य में एक दृश्य मॉडल को बांधने की आवश्यकता है।

कुछ की तरह अधिक वर्णनात्मक करने के लिए अपने EmailRequest मॉडल बदलें:

public class EmailRequestViewModel 
{ 
    [Required(ErrorMessage = "Required")] 
    public string Email { get; set; } 
} 

आपका get कार्रवाई विधि कुछ ऐसा दिखाई देगा:

public ActionResult EmailRequest() 
{ 
    EmailRequestViewModel viewModel = new EmailRequestViewModel(); 

    return View(viewModel); 
} 

आपका पोस्ट कार्रवाई विधि:

public ActionResult EmailRequest(EmailRequestViewModel viewModel) 
{ 
    // Check for null view model 

    if (!ModelState.IsValid) 
    { 
      return View(viewModel); 
    } 

    // Do whatever you need to do 

    return RedirectToAction("List"); 
} 

और फिर आपका विचार। कृपया ASP.NET MVC 4 कोड बहाना, MVC 2 प्रागैतिहासिक है :) यह आपके विचार का सिर्फ एक हिस्सा है:

@model YourProject.ViewModels.EmailRequestViewModel 

@using (Html.BeginForm()) 
{ 
    @Html.TextBoxFor(x => x.Email) 
    @Html.ValidationMessageFor(x => x.Email) 
} 

मुझे आशा है कि इस मदद करता है।

-1

आप आप के लिए `EmailRequest model` और फिर email`` स्ट्रिंग से अपनी पोस्ट कार्रवाई के हस्ताक्षर बदलते हैं तो पहले Modelstat.IsValid

public ActionResult EmailRequest() 
    { 
      EmailRequest email = new EmailRequest(); 
      TryUpdateModel(email); 
     if (ModelState.IsValid) 
     { 
      // save to db, for instance 
      return RedirectToAction("AnotherAction"); 
     } 
     return View(); 
    } 
संबंधित मुद्दे