2016-03-26 18 views
1

Ich habe Modell alsBenutzerdefinierte Validierung in MVC Modell

public class GroupedIssueData 
{ 

    [Range(0, double.MaxValue, ErrorMessage = "Please enter valid number")] 
    public double IssueQty { get; set; } 

    public double ReqQty { get; set; } 

    public bool isSavings { get; set; } 
} 

Diese beiden Eigenschaften als IssueQty und IsSaving enthält, Wenn der IsSaving geprüft wird, dann kann IssueQty leer sein, wenn die IssueQty nicht leer ist, dann kann IsSaving sein leer gelassen. Wie kann ich bestätigen diese

My View

<td> 
    @Html.DisplayFor(m => m.MaterialData[i].ReqQty) 
    @Html.HiddenFor(m => m.MaterialData[i].ReqQty) 
</td> 
<td>@Html.TextBoxFor(m => m.MaterialData[i].IssueQty, new { style = "width:70px" })@Html.ValidationMessageFor(m => m.MaterialData[i].IssueQty)</td> 
<td class="text-center">@Html.CheckBoxFor(m => m.MaterialData[i].isSavings)</td> 

Und mein Controller ist

public async Task<ActionResult> GetWorkOrderMaterialDetails(IssueEntryModel m) 
{ 
    if (!ModelState.IsValid) 
    { 
     // redirect 
    } 
    var model = new IssueEntryModel(); 
} 

ist Wie kann ich auf die umleiten, wenn das Modell nicht gültig ist. Muss ich zum selben Controller umleiten. Ich möchte die eingegebenen Daten beibehalten.

Meine Ansicht ist

+0

Verwenden Sie einen [narrensicher] (http://foolproof.codeplex.com /) '[RequiredIfTrue]' oder ähnliches Validierungsattribut (oder schreiben Sie Ihr eigenes) –

Antwort

0

versuchen, diese

`

[Required] 
     [Range(18, 100, ErrorMessage = "Please enter an age between 18 and 50")] 
     public int Age { get; set; } 


    [Required]   
    [StringLength(10)] 
    public int Mobile { get; set; }    

    [Range(typeof(decimal), "0.00", "15000.00")] 
    public decimal Total { get; set; } ` 

if (ModelState.IsValid) 
     { 
      // 
     } 
     return View(model); 

Validation to the Model

Custom Validation Data Annotation Attribute

0

Sie können eine benutzerdefinierte Validierung vornehmen z.B. RequiredIfOtherFieldIsNullAttribute wie hier beschrieben:

How to validate one field related to another's value in ASP .NET MVC 3

public class RequiredIfOtherFieldIsNullAttribute : ValidationAttribute,  IClientValidatable 
{ 
private readonly string _otherProperty; 
public RequiredIfOtherFieldIsNullAttribute(string otherProperty) 
{ 
    _otherProperty = otherProperty; 
} 

protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
{ 
    var property = validationContext.ObjectType.GetProperty(_otherProperty); 
    if (property == null) 
    { 
     return new ValidationResult(string.Format(
      CultureInfo.CurrentCulture, 
      "Unknown property {0}", 
      new[] { _otherProperty } 
     )); 
    } 
    var otherPropertyValue = property.GetValue(validationContext.ObjectInstance, null); 

    if (otherPropertyValue == null || otherPropertyValue as string == string.Empty) 
    { 
     if (value == null || value as string == string.Empty) 
     { 
      return new ValidationResult(string.Format(
       CultureInfo.CurrentCulture, 
       FormatErrorMessage(validationContext.DisplayName), 
       new[] { _otherProperty } 
      )); 
     } 
    } 

    return null; 
} 

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) 
{ 
    var rule = new ModelClientValidationRule 
    { 
     ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()), 
     ValidationType = "requiredif", 
    }; 
    rule.ValidationParameters.Add("other", _otherProperty); 
    yield return rule; 
} 

}

Und es so verwenden:

[RequiredIfOtherFieldIsNull("IsSavings")] 
public double IssueQty { get; set; } 
[RequiredIfOtherFieldIsNull("IssueQty")] 
public bool IsSavings { get; set; } 
0

Verwenden IValidatableObject

jedoch Ihre Bedingung: Wenn der IsSaving wird geprüft dann ist sueQty kann leer sein, wenn die IssueQty nicht leer ist, dann kann IsSaving leer gelassen werden wenig verwirrend ist, aber dies könnte man andeuten sowieso

public class GroupedIssueData : IValidatableObject 
{ 
    [Range(0, double.MaxValue, ErrorMessage = "Please enter valid number")] 
    public double IssueQty { get; set; } 

    public double ReqQty { get; set; } 

    public bool isSavings { get; set; } 

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) 
    { 
     if (!isSavings && IssueQty == 0) 
     { 
      yield return new ValidationResult("Error Message"); 
     } 
    } 
} 

public async Task<ActionResult> GetWorkOrderMaterialDetails(IssueEntryModel m) 
{ 
    if (!ModelState.IsValid) 
    { 
     return View(m); 
     // redirect 
    } 

} 
+0

Danke, Wie umzuleiten, um die Validierungsergebnisse anzuzeigen, ohne die Daten zu verlieren. Muss ich über denselben oder einen anderen Controller zurückkehren? – Techonthenet

+0

@Techonthenet Bitte überprüfen Sie meine Änderungen – brykneval

Verwandte Themen