खोज…


ActionResult में मान्य मॉडल

[HttpPost]
public ActionResult ContactUs(ContactUsModel contactObject)
{
    // This line checks to see if the Model is Valid by verifying each Property in the Model meets the data validation rules
    if(ModelState.IsValid)
    {
    }
    return View(contactObject);
}

मॉडल वर्ग

public class ContactUsModel
{
    [Required]
    public string Name { get; set; }
    [Required]
    [EmailAddress] // The value must be a valid email address
    public string Email { get; set; }
    [Required]
    [StringLength(500)] // Maximum length of message is 500 characters
    public string Message { get; set; }
}

किसी ऑब्जेक्ट को सत्यापन से निकालें

कहो कि आपके पास निम्न मॉडल है:

public class foo
{
    [Required]
    public string Email { get; set; }

    [Required]
    public string Password { get; set; }

    [Required]
    public string FullName { get; set; }
}

लेकिन आप FullName को Modelvalidation से बाहर करना चाहते हैं क्योंकि आप मॉडल का उपयोग उस स्थान पर भी कर रहे हैं जहाँ FullName भरा नहीं है, आप निम्न तरीके से ऐसा कर सकते हैं:

ModelState.Remove("FullName");

कस्टम त्रुटि संदेश

यदि आप कस्टम त्रुटि संदेश प्रदान करना चाहते हैं तो आप इसे इस तरह करेंगे:

public class LoginViewModel
{
    [Required(ErrorMessage = "Please specify an Email Address")]
    [EmailAddress(ErrorMessage = "Please specify a valid Email Address")]
    public string Email { get; set; }
    
    [Required(ErrorMessage = "Type in your password")]
    public string Password { get; set; }
}

जब आपके त्रुटि संदेश एक ResourceFile (.resx) में हैं, तो आपको ResourceType और ResourceName को निर्दिष्ट करना होगा:

public class LoginViewModel
{
    [Required(ErrorMessageResourceType = typeof(ErrorResources), ErrorMessageResourceName = "LoginViewModel_RequiredEmail")]
    [EmailAddress(ErrorMessageResourceType = typeof(ErrorResources), ErrorMessageResourceName = "LoginViewModel_ValidEmail")]
    public string Email { get; set; }
    
    [Required(ErrorMessageResourceType = typeof(ErrorResources), ErrorMessageResourceName = "LoginViewModel_RequiredPassword")]
    public string Password { get; set; }
}

मॉडल और नियंत्रक में कस्टम त्रुटि संदेश बनाना

मान लीजिए कि आपके पास निम्न वर्ग है:

public class PersonInfo
{
    public int ID { get; set; }

    [Display(Name = "First Name")]
    [Required(ErrorMessage = "Please enter your first name!")]
    public string FirstName{ get; set; }

    [Display(Name = "Last Name")]
    [Required(ErrorMessage = "Please enter your last name!")]
    public string LastName{ get; set; }

    [Display(Name = "Age")]
    [Required(ErrorMessage = "Please enter your Email Address!")]
    [EmailAddress(ErrorMessage = "Invalid Email Address")]
    public string EmailAddress { get; set; }
}

यदि आपका ModelState.IsValid रिटर्न गलत है, तो ये कस्टम त्रुटि संदेश दिखाई देंगे।

लेकिन, आप और साथ ही मुझे पता है कि प्रति व्यक्ति केवल 1 ईमेल पता हो सकता है, या फिर आप संभावित रूप से गलत लोगों और / या कई लोगों को ईमेल भेज रहे होंगे। यह वह जगह है जहाँ नियंत्रक में जाँच खेल में आता है। तो चलिए मान लेते हैं कि लोग क्रिएट एक्शन के जरिए आपके लिए अकाउंट्स बना रहे हैं।

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID, FirstName, LastName, EmailAddress")] PersonInfo newPerson)
{
    if(ModelState.IsValid) // this is where the custom error messages on your model will display if return false
    {
        if(database.People.Any(x => x.EmailAddress == newPerson.EmailAddress))  // checking if the email address that the new person is entering already exists.. if so show this error message
        {
            ModelState.AddModelError("EmailAddress", "This email address already exists! Please enter a new email address!");
            return View(newPerson);
        }
        
        db.Person.Add(newPerson);
        db.SaveChanges():
        return RedirectToAction("Index");
    }

    return View(newPerson);
}

मुझे आशा है कि यह किसी की मदद करने में सक्षम है!

JQuery में मॉडल सत्यापन।

ऐसे मामलों में जहां आपको Jquery का उपयोग करके मॉडल सत्यापन सुनिश्चित करने की आवश्यकता होती है, .valid () फ़ंक्शन का उपयोग किया जा सकता है।

मॉडल वर्ग फ़ील्ड

[Required]
[Display(Name = "Number of Hospitals")]
public int Hospitals{ get; set; }
[Required]
[Display(Name = "Number of Beds")]
public int Beds { get; set; }

कोड देखें

@using (Html.BeginForm(new {id = "form1", @class = "form-horizontal" }))
{

<div class="divPanel">
  <div class="row">
    <div class="col-md-3">                
            @Html.LabelFor(m => m.Hospitals)
            @Html.TextBoxFor(m => m.Hospitals, new { @class = "form-control", @type = "number"})
            @Html.ValidationMessageFor(m => m.Hospitals)

    </div>
    <div class="col-md-3">

            @Html.LabelFor(m => m.Beds)
            @Html.TextBoxFor(m => m.Beds, new { @class = "form-control", @type = "number"})
            @Html.ValidationMessageFor(m => m.Beds)
    </div>
<div class="col-md-3">             
        <button type=button  class="btn btn-primary" id="btnCalculateBeds"> Calculate Score</button>
    </div>
 </div>

  </div>
 }

सत्यापन जाँच के लिए स्क्रिप्ट।

$('#btnCalculateBeds').on('click', function (evt) {
evt.preventDefault();

if ($('#form1').valid()) {
//Do Something.
}
}

सुनिश्चित करें कि jquery.validate और jquery.validate.unobtrusive फाइलें समाधान में मौजूद हैं।



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow