简体   繁体   中英

How to disabled selected date (asp.net mvc4)

I am trying to make an appointment page in MVC4. It is working well but I would like to disable the date which is chosen. Here is my controller to make an appointment:

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");
       }
       //
   }
}

and here is my view with the datepicker

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

seems to me your TextBoxFor for "Date" may be disable depends on condition. you may try this.

@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>
@Html.TextBoxFor(u => u.Date, htmlAttributes: new { id = "DatePicker", disabled="disabled" })

要么

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

Please try it on JavaScript code.. Hope so it helps..

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

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

    });

</script>

this above is to disable a all datepicker whose id: #DatePicker So,update code after your comment:

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

and arrayDisabledDates is array of dates

var arrayDisabledDates=[...,...,..]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM