2012-08-07 37 views
1

我有一些複選框需要在用戶轉到下一頁之前進行檢查。顯示驗證消息的最佳方式是什麼?確認不使用模型時檢查複選框

查看:

@Html.CheckBox("terms_eligibility") 
@Html.CheckBox("terms_accurate") 
@Html.CheckBox("terms_identity_release") 
@Html.CheckBox("terms_score_release") 

控制器:

[HttpPost] 
public ActionResult Review(ReviewModel model) 
{ 
    // Make sure all Terms & Conditions checkboxes are checked 
    var termsEligibility = Request.Form.GetValues("terms_eligibility")[0]; 
    var termsAccurate = Request.Form.GetValues("terms_accurate")[0]; 
    var termsIdentityRelease = Request.Form.GetValues("terms_identity_release")[0]; 
    var termsScoreRelease = Request.Form.GetValues("terms_score_release")[0]; 

    if (termsEligibility == "true" && termsAccurate == "true" && 
     termsIdentityRelease == "true" && termsScoreRelease == "true") 
    { 
     return RedirectToAction("CheckOut","Financial"); 
    } 

    return null; 
} 

編輯,

我提出建議的更改。現在我如何獲得相同的頁面以顯示錯誤消息?

我改變了模型的屬性本

[RequiredToBeTrue(ErrorMessage = "*")] 

這裏的電腦板

[HttpPost] 
public ActionResult Review(ReviewModel model) 
{ 


    if(ModelState.IsValid) 
    { 
     return RedirectToAction("CheckOut", "Financial"); 
    } 

    return RedirectToAction("Review"); 
} 

回答

2

我建議你使用一個視圖模型和一個自定義的驗證屬性:

public class RequiredToBeTrueAttribute : RequiredAttribute 
{ 
    public override bool IsValid(object value) 
    { 
     return value != null && (bool)value; 
    } 
} 

和查看模型:

public class MyViewModel 
{ 
    [RequiredToBeTrue] 
    public bool TermsEligibility { get; set; } 

    [RequiredToBeTrue] 
    public bool TermsAccurate { get; set; } 

    [RequiredToBeTrue] 
    public bool TermsIdentityRelease { get; set; } 

    [RequiredToBeTrue] 
    public bool TermsScoreRelease { get; set; } 

    ... some other properties that are used in your view 
} 

,當然還有視圖將強類型的視圖模型:

@model MyViewModel 

@Html.ValidationSummary(false) 
@using (Html.BeginForm()) 
{ 
    @Html.CheckBoxFor(x => x.TermsEligibility) 
    @Html.CheckBoxFor(x => x.TermsAccurate) 
    @Html.CheckBoxFor(x => x.TermsIdentityRelease) 
    @Html.CheckBoxFor(x => x.TermsScoreRelease) 
    ... 
    <button type="submit">OK</button> 
} 

,最後你的控制器動作將採取視圖模型參數:

[HttpPost] 
public ActionResult Review(MyViewModel model) 
{ 
    if (!ModelState.IsValid) 
    { 
     // there were validation errors => redisplay the same view 
     // so that the user could fix them 
     return View(model); 
    } 

    // at this stage we know that validation succeeded => 
    // we could proceed in processing the data and redirecting 
    ... 

    return RedirectToAction("CheckOut", "Financial"); 
} 

注意之類的東西Request.Form.GetValues("terms_eligibility")[0];termsEligibility == "true"實際上並不是您希望在正確編寫的應用程序中看到的那種代碼。

+0

感謝您的幫助。如果驗證失敗,如何顯示錯誤消息。 – 2012-08-07 16:35:22

+1

這就是我的例子中@ Html.ValidationSummary(false)'helper所做的事情。但是,如果您想爲每個複選框執行此操作,則還可以使用每個複選框旁邊的「ValidationMessageFor」助手。例如:'@ Html.ValidationMessageFor(x => x.TermsEligibility)'。 – 2012-08-07 16:36:55

+0

謝謝我越來越近了。有兩個問題1.當驗證失敗時,摘要確實顯示錯誤,但是whol頁面沒有任何CSS樣式,它們都是黑白文本。 – 2012-08-07 18:20:23