【问题标题】:How to disabled selected date (asp.net mvc4)如何禁用选定的日期(asp.net mvc4)
【发布时间】:2014-05-06 08:32:08
【问题描述】:

我正在尝试在 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>

【问题讨论】:

  • 不确定这是不是你想要的@Html.TextBoxFor(u =&gt; u.Date, htmlAttributes: new { id = "DatePicker", disabled="true" })
  • @Kunukn: readonly=true 在这里可能更合适。更好的可读性,并且该值仍然与表单一起提交。
  • @Flater readonly 是保留关键字,所以是@readonly=true :)
  • @Kunukn:准点。我通常只在 VS 提醒我时才注意到这些类型的事情:)

标签: c# jquery asp.net asp.net-mvc asp.net-mvc-4


【解决方案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>

【讨论】:

  • 但是我在哪里可以添加我到数据库的连接来检查是否已经选择了那个日期?
  • 在这种情况下,if 条件可能类似于 if(Model.Date>DateTime.MinValue) 或类似的东西
【解决方案2】:
@Html.TextBoxFor(u => u.Date, htmlAttributes: new { id = "DatePicker", disabled="disabled" })

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

【讨论】:

  • @Html.TextBoxFor(u => u.Date, htmlAttributes: new { id = "DatePicker", @readonly="true" }) ...我用过这个。我选择22 May并在我去约会页面 22 时创建的约会可能未被禁用????
  • 对于您的情况,您应该使用disabled="disabled"。因为 readonly 将使文本只读,但 datepicker 事件仍然可以工作并且可以更改日期。使用disabled="disabled" 告诉我它是否有效。
【解决方案3】:

请在 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=[...,...,..]

【讨论】:

  • 好的,我想我解释错了。这段代码禁用了我所有的日期选择器。我想验证一下,不再选择选定的日期。每个约会的每一天。所以首先,所有日期都应该可用,但是当有人点击 6 月 15 日(只是一个例子)并获得约会时。没有人可以再次选择 6 月 15 日。我想我应该使用与我的数据库连接的代码,因为我在我的数据库中保留约会日期。
  • 但是我在哪里可以添加我到数据库的连接来检查是否已经选择了那个日期?
猜你喜欢
  • 1970-01-01
  • 2014-10-09
  • 1970-01-01
  • 2020-01-11
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多