【发布时间】:2017-12-04 09:57:37
【问题描述】:
每次用户按下“订单”按钮时,我都会尝试在模式对话框中获取实际时间。
<a class="btn btn-primary" data-toggle="modal" data-target="#OrderModal">@MEDONET.Language.Doctor.Texts.Order</a>
所以每次他在模态 div OrderModal 中执行此操作
<div id="OrderModal" class="modal fade" role="dialog">
@Html.Action("CreateOrderForm", Model.CreateOrderVM)
</div>
调用 CreateOrderForm 操作
public ActionResult CreateOrderForm(CreateOrderVM createOrderVM)
{
createOrderVM.Minute = DateTime.Now.Minute.ToString().PadLeft(2, '0');
return View("CreateOrderForm", createOrderVM);
}
因此该操作实际上每次都在运行并调用 CreateOrderForm.cshtml 文件
@{
Layout = null;
}
@model MEDONET.Models.CreateOrderVM
<div class="modal-dialog modal-sm">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title text-center">Order To Line</h4>
</div>
<div class="modal-body">
@using (Html.BeginForm("CreateOrder", "Doctors", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.DoctorId)
<div class="row text-center">
<p>Date & Time</p>
@Html.Label("Year")
@Html.EditorFor(model => model.Year, new { htmlAttributes = new { @class = "form-control", @Value = DateTime.Now.Year } })<br />
@Html.Label("Month")
@Html.EditorFor(model => model.Month, new { htmlAttributes = new { @class = "form-control", @Value = DateTime.Now.Month } })<br />
@Html.Label("Day")
@Html.EditorFor(model => model.Day, new { htmlAttributes = new { @class = "form-control", @Value = DateTime.Now.Day } })<br />
@Html.Label("Hour")
@Html.EditorFor(model => model.Hour, new { htmlAttributes = new { @class = "form-control", @Value = DateTime.Now.Hour } })<br />
@Html.Label("Minute")
@Html.EditorFor(model => model.Minute, new { htmlAttributes = new { @class = "form-control" } })<br />
@if (this.User.IsInRole("Registrator"))
{
@Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })<br />
}
<button type="submit" class="btn btn-default">Submit</button>
</div>
}
</div>
<div class="modal-footer">
</div>
</div>
但是!弹出字段仍然填充父页面加载时给出的第一次。
如何在用户每次按下订单按钮时给他新的时间?
【问题讨论】:
-
每次单击按钮时调用该方法的代码在哪里? (
@Html.Action("CreateOrderForm", Model.CreateOrderVM)只是在您第一次渲染页面时调用它) -
附带说明,在使用
HtmlHelper方法时,切勿设置value属性 - 您设置绑定到的属性的值。 -
另外,应该有
[HttpGet]和[HttpPost]修饰的方法吧? -
是的,就是post的动作 [HttpPost] [ValidateAntiForgeryToken] public ActionResult CreateOrder(CreateOrderVM createOrderVM) { *** }
-
我尝试从 javascript 调用它。它确实有效,每次我按下按钮时它都会调用动作并且动作会更改模型,但字段继续保持旧值。我的意思是分钟。
$.ajax({ url: '@Url.Action("CreateOrderForm","Doctors")', error: function (xhr, ajaxOptions, thrownError) { //some errror, some show err msg to user and log the error alert(xhr.responseText); } });
标签: javascript jquery asp.net asp.net-mvc razor