2015-05-28 89 views
0

我有這些變量:做一個必填字段,如果其他字段值

public int? BossId { get; set; } 
public DateTime? HeadShipDate { get; set; } 

我的觀點:

<div class="form-group"> 
    @Html.LabelFor(model => model.BossId, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.DropDownList("BossId", null, "-- Select --", htmlAttributes: new { @class = "form-control" }) 
     @Html.ValidationMessageFor(model => model.BossId, "", new { @class = "text-danger" }) 
    </div> 
</div> 

<div id="divDate" class="form-group"> 
    @Html.LabelFor(model => model.HeadShipDate, htmlAttributes: new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(model => model.HeadShipDate, new { htmlAttributes = new { @class = "form-control" } }) 
     @Html.ValidationMessageFor(model => model.HeadShipDate, "", new { @class = "text-danger" }) 
    </div> 
</div> 

腳本(我是想如果BossId有一個值,使所需) :

<script type="text/javascript"> 
     HeadShipDate: { 
       required: { 
        depends: function(element){ 
         return $("#BossId").text() != '-- Select --'; 
        } 
       } 
     } 
</script> 

控制器:

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create([Bind(Include = "Id,Name,Acronym,Percentage,BossId,HeadShipDate")] Department department) 
{ 
    Seller boss = db.Sellers.Find(department.BossId); 
    if (boss.AdmissionDate > department.HeadShipDate) 
     ModelState.AddModelError(string.Empty, "Headship Date (" + department.HeadShipDate.Value.ToShortDateString() + ") must be greater than the Admission Date (" + boss.AdmissionDate.ToShortDateString() + ") of boss"); 
    else if (ModelState.IsValid) 
    { 
     boss.DeptId = department.Id; 
     db.Departments.Add(department); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    ViewBag.BossId = new SelectList(SellersNonBosses(), "Id", "Name", department.BossId); 
    return View(department); 
} 

我想讓HeadShipDate爲必填字段如果在其他作品中,如果DropDownList的文本不是「 - Select - 」,那麼IF BossId有一個值,並且我想進行驗證在控制器中,使用JavaScript檢查與特定銷售商(Boss)的入場日期相關的頭銜日期),我該如何做到這一點?

+0

什麼是錯的 「工作正常」 的事情嗎? –

+0

我不問腳本1,我只是顯示我的代碼,我正在尋找解決方案腳本2和一個腳本示例,以驗證(在控制器)與JS。你現在做了嗎?順便說一句,我會編輯.. – developer033

回答

1

在您的視圖模型上實現IValidatableObject可以更好地控制像這樣的臨時驗證規則。

public class Department : IValidatableObject 
{ 
    public int? BossId { get; set; } 
    public DateTime? HeadShipDate { get; set; } 
    ... 

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) 
    { 
     if(BossId.HasValue && !HeadShipDate.HasValue) 
     { 
      yield return new ValidationResult("HeadShipDate is required if BossId is selected.", new[] { "HeadShipDate" }); 
     } 
    } 
} 

然後你只需要在你的控制器行動檢查ModelState.IsValid

注意:這隻適用於模型狀態的服務器端驗證。如果你想要客戶端驗證,你需要實現一個Javascript函數,在提交表單之前做類似的事情。

例子:

$('form').validate({ 
    rules: { 
     HeadShipDate: { 
      required: { 
       depends: function (element) { 
        return $("#BossId").val() != ''; 
       } 
      } 
     } 
    }, 
    messages: { 
     HeadShipDate: { 
      required: "Please specify the Headship Date" 
     } 
    } 
}); 
+0

我要求客戶端驗證,無論如何,ModelState.IsValid會自動調用這個類「MyViewModel」?你看過我的劇本2嗎?我錯過了什麼? [如果BossId有值,則需要headShip] ..謝謝。 – developer033

+0

是的,'ModelState'會自動獲得MyViewModel的Validate方法的結果。 – kspearrin

+0

嗯,在我的情況下,它不是..我把斷點看,什麼也沒有.. – developer033

相關問題