मुझे समझ में नहीं आता क्यों 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; }
}
}
द्वारा यह chek करने की क्षमता है करने के लिए बांधने की मशीन के साथ अपने मॉडल के लिए बाध्य करने की जरूरत है राज्य की जांच करें, आप क्या परिणाम देखते हैं? –
[HttpPost] public ActionResult EmailRequest (EmailRequest ईमेल) { अगर (ModelState.IsValid) { // db से, उदाहरण के वापसी RedirectToAction ("AnotherAction") को बचाने के लिए; } वापसी देखें(); } – Dave
जैसा कि आपने मुझे सुझाव दिया है, मैंने स्ट्रिंग से ईमेलरक्वेट में बदल दिया है और यह शून्य – Dave