2016-03-26 39 views
1

私はIssueQtyが空でない場合IsSavingが、その後、IssueQtyを空にすることができますチェックされている場合、これは、IssueQtyとIsSavingなどの2つのプロパティが含まれていカスタム検証モデル

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; } 
} 

としてモデルは、その後IsSavingをすることができたのです空白のまま。どのように私はこの

マイビューが

<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> 

で検証そして、私のコントローラは、モデルが有効でない場合、私はにリダイレクトするにはどうすればよい

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

であることができます。同じコントローラにリダイレクトする必要がありますか?入力したデータを保持したい。

マイビューが

+0

をヒントかもしれない[誰にでもできる](http://foolproof.codeplex.comを使用して空白のままにすることができます/) '[RequiredIfTrue]'または類似の検証属性 –

答えて

0

あなたはカスタム検証などを行うことができます。この

`

[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

を試しています

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; 
} 

}

そして、そのようにそれを使用します:

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

使用IValidatableObject

しかし、あなたの条件:IsSavingがチェックされている場合は、ここで説明したようにRequiredIfOtherFieldIsNullAttribute次にIs sueQtyを空にすることができIssueQtyが空でない場合は、IsSavingはは少し混乱しているが、これはあなたにとにかく

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

ありがとう、どのようにデータを紛失することなく検証結果を表示するためにリダイレクトするのですか?同じコントローラまたは別のコントローラを介して返却する必要がありますか? – Techonthenet

+0

@Techonthenet私の編集内容を確認してください – brykneval