2014-05-06 36 views
0

我想在MVC4中預約頁面。它運行良好,但我想禁用所選的日期。這裏是我的控制器預約:如何禁用選定日期(asp.net mvc4)

public ActionResult Make() 
     { 
      return View(); 
     } 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Make(Models.AppModel User) 
     { 
      if (Session["UserEmail"] != null) 
      { 
       using (var db = new MaindbModelDataContext()) 
       { 
        var patient = db.Patients.FirstOrDefault(u => u.Email == (String)Session["UserEmail"]); 

        var app = new Appointment(); 

        app.Date = (DateTime)User.Date; 
        app.Description = User.Description; 
        app.Status = "isPending"; 
        app.PatientNo = patient.PatientNo; 
        app.AppNo = Guid.NewGuid().GetHashCode(); 
        db.Appointments.InsertOnSubmit(app); 
        db.SubmitChanges(); 

       } 
      } 
      else 
      { 
       return RedirectToAction("Index", "User"); 
      } 
      return RedirectToAction("Index", "Patient"); 
     } 
     // 
    } 
} 

,這裏是我和日期選擇器視圖

@model DentAppSys.Models.AppModel 
@{ 
    ViewBag.Title = "Appointment"; 
} 
<link type="text/css" rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"> 
<script src="//code.jquery.com/jquery-1.10.2.js"></script> 
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script> 

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken(); 
    @Html.ValidationSummary(true, ""); 
    <div> 

     <fieldset> 
      <legend>Get an Appointment</legend> 


      <div>@Html.LabelFor(u => u.Date)</div> 
      <div> 
       @Html.TextBoxFor(u => u.Date, htmlAttributes: new { id = "DatePicker" }) 
       @Html.ValidationMessageFor(u => u.Date) 

      </div> 

      <div>@Html.LabelFor(u => u.Description)</div> 
      <div> 
       @Html.TextBoxFor(u => u.Description) 

      </div> 

      <input type="submit" value="Submit" class="footer_btn" /> 

     </fieldset> 

    </div> 
} 
<script type="text/javascript"> 
    $(function() { 
     $("#DatePicker").datepicker(); 
    }); 

</script> 
+0

不知道這是否是你想要的'@ Html.TextBoxFor(U => u.Date,htmlAttributes:新{ID = 「的DatePicker」,禁用= 「真正的」})' – Kunukn

+0

@Kunukn:' readonly = true'在這裏可能更合適。更好的可讀性,價值仍然與表單一起提交。 – Flater

+1

@Flater只讀是一個保留關鍵字,所以它是@ readonly = true :) – Kunukn

回答

0
@Html.TextBoxFor(u => u.Date, htmlAttributes: new { id = "DatePicker", disabled="disabled" }) 

@Html.TextBoxFor(u => u.Date, htmlAttributes: new { id = "DatePicker", @readonly="true" }) 
+0

@ Html.TextBoxFor(u => u.Date,htmlAttributes:new {id =「DatePicker」,@ readonly =「true」}) ... I使用這一個。我選擇22月5日,並創建約會時,我去約會第22頁可能沒有禁用? – user1953051

+0

對於你的情況,你應該使用'disabled =「disabled」'。只讀只會使文本只讀,但datepicker事件仍然有效,並且可以更改日期。使用'disabled =「disabled」'讓我知道它是否有效。 – Yasser

0

請嘗試在JavaScript代碼.. 希望對你有所幫助..

<script type="text/javascript"> 
    $(function() { 
     $("#DatePicker").datepicker(); 

     $("#DatePicker").attr('disabled','disabled') 

    }); 

</script> 

這上面是禁用所有的日期選擇器,其ID:#DatePicker您的評論後 因此,更新代碼:

$('#DatePicker').datepicker({ 
    beforeShowDay: function (dt) { 
     var bDisable = arrayDisabledDates[dt]; 
     if (bDisable) 
      return [false, '', '']; 
     else 
      return [true, '', '']; 
    } 
}); 

和arrayDisabledDates是日期的數組

VAR arrayDisabledDates = [.. 。,...,..]

+0

好吧,我想我解釋它wrong.This代碼禁用了我所有的datepicker.I想做一個驗證,不再選擇選定的一天每一天每個約會。所以首先,所有的日期應該是可用的,但當有人點擊6月15日只是一個例子),並得到約會。沒有人可以再次選擇15月6日。我想我應該使用與我的數據庫連接的代碼,因爲我在我的數據庫中保留約會日期。 – user1953051

+0

但我在哪裏可以添加我的連接到數據庫來檢查該日期是否已被選中? – user1953051

1

對我來說你的TextBoxFor對於「日期」可能是禁用的,取決於條件。 你可以試試這個。

@using (Html.BeginForm()) 
     { 
      @Html.AntiForgeryToken(); 
      @Html.ValidationSummary(true, ""); 
      <div> 

       <fieldset> 
        <legend>Get an Appointment</legend> 


        <div>@Html.LabelFor(u => u.Date)</div> 
         <div> 
     @{ 
string idAttr=""; 
     if(condition_for_disable==true) 
     { 
idAttr="DatePicker"; 
} 
          @Html.TextBoxFor(u => u.Date, htmlAttributes: new { id = idAttr }) 

      }     @Html.ValidationMessageFor(u => u.Date) 

         </div> 

        <div>@Html.LabelFor(u => u.Description)</div> 
        <div> 
         @Html.TextBoxFor(u => u.Description) 

        </div> 

        <input type="submit" value="Submit" class="footer_btn" /> 

       </fieldset> 

      </div> 
     } 


<script type="text/javascript"> 
    $(function() { 
     $("#DatePicker").datepicker(); 
    }); 

</script> 
+0

但我在哪裏可以添加我的連接到數據庫來檢查是否已選擇該日期? – user1953051

+0

在這種情況下,如果條件可能是像(Model.Date> DateTime.MinValue)或類似的東西 – Najib

相關問題