简体   繁体   English

ASP.NET MVC5剃刀刷新模式对话框

[英]ASP.NET MVC5 Razor Refresh modal dialog

I'm tryin' to get actual time in modal dialog every time a user press the Order button. 每当用户按下“订购”按钮时,我都会尝试在模态对话框中获取实际时间。

<a class="btn btn-primary" data-toggle="modal" data-target="#OrderModal">@MEDONET.Language.Doctor.Texts.Order</a>

So every time he do this in the modal div OrderModal 因此,每次他在模式div OrderModal中执行此操作时,

<div id="OrderModal" class="modal fade" role="dialog">
    @Html.Action("CreateOrderForm", Model.CreateOrderVM)
</div>

Which calls to CreateOrderForm action 哪个调用CreateOrderForm操作

public ActionResult CreateOrderForm(CreateOrderVM createOrderVM)
    {
        createOrderVM.Minute = DateTime.Now.Minute.ToString().PadLeft(2, '0');
        return View("CreateOrderForm", createOrderVM);
    }

So that action is actually running every time and calls CreateOrderForm.cshtml file 因此该操作实际上每次都在运行,并调用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">&times;</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>

BUT! 但! The popup fields are still filled with the first time given while parent page loading. 弹出字段仍填充有父页面加载时给定的第一次时间。

How can I give a user new time every time he press the Order button? 每当用户按下“订购”按钮时,如何给用户新的时间?

Razor code is executed only once, when page loads first time. 第一次加载页面时,Razor代码仅执行一次。 After that, each time you toggle the modal popup, it just show/hide the same HTML. 之后,每次您切换模式弹出窗口时,它只会显示/隐藏相同的HTML。 The solution would be to update the HTML, once you click the button to open the Modal. 解决方案是在您单击按钮以打开Modal后更新HTML。 You can use JQuery's load() method to achieve this. 您可以使用JQuery的load()方法来实现。

First you need to use JavaScript to toggle the Modal instead of HTML. 首先,您需要使用JavaScript而不是HTML来切换模态。 Modify your button like below 如下修改按钮

<a class="btn btn-primary" id="openModal">@MEDONET.Language.Doctor.Texts.Order</a>

In JavaScript, add the event handler for this button like below 在JavaScript中,为该按钮添加事件处理程序,如下所示

$('#openModal').on('click', function() {
  ...
})

Then, load the content of child action. 然后,加载子动作的内容。

$('#openModal').on('click', function() {
  $("#OrderModal").load("@Url.Action("CreateOrderForm", "ControllerName")", function() {
    $('#OrderModal').modal('show')
  });
})

I haven't tested above code, and it may need few typo correction. 我尚未测试以上代码,因此可能需要很少的错字校正。 Let me know, if it doesn't work. 让我知道,如果它不起作用。

I'm guessing that the method use some kind of post and than returns the new form. 我猜该方法使用某种形式的帖子,而不是返回新表格。 In this cases it happens that when you return the same view the model state is the one that populate those fields. 在这种情况下,当您返回相同的视图时,模型状态就是填充这些字段的状态。 In order to solve it add in the action 为了解决它添加动作

ModelState.Clear();

Or 要么

ModelState.Remove("myChangeValue");

you can look for more info in https://blogs.msdn.microsoft.com/simonince/2010/05/05/asp-net-mvcs-html-helpers-render-the-wrong-value/ 您可以在https://blogs.msdn.microsoft.com/simonince/2010/05/05/asp-net-mvcs-html-helpers-render-the-wrong-value/中查找更多信息

If you just want to change the time in the modal I would recommend to change it with javascript when you show the modal. 如果您只想更改模式中的时间,建议您在显示模式时使用javascript进行更改。

$('#OrderModal #Minute').val(new Data().getMinutes());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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