如何在mvc 5方法中从引导模式popoup获取输入值

本文关键字:模式 popoup 获取 输入 mvc 方法 | 更新日期: 2023-09-27 18:22:17

请告诉我如何从mvc 5方法的引导模式弹出窗口中获取输入值,我正在使用以下命令从按钮点击事件中调用该方法:

<button type="button" class="btn btn-blue" onclick="location.href = '@Url.Action("SaveToDb")';return false;">
public void SaveToDb(AddressViewModel addrs)
    {
        var addressLine1 = addrs.AddressLine1;
        var addressLine2 = addrs.AddressLine2;
        var city = addrs.City;
    }

我的表单:

<form role="form" class="form-horizontal" method="POST">
<!-- start: BOOTSTRAP EXTENDED MODALS -->
<div id="responsive" class="modal fade" tabindex="-1" data-width="760" style="display: none; top: 50px;">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
            &times;
        </button>
        <h4 class="modal-title">Add Address</h4>
    </div>
    <div class="modal-body">
        <div class="form-group">
            @Html.LabelFor(model => model.ClientId, htmlAttributes : new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ClientId, new { htmlAttributes = new { @class = "form-control", @ReadOnly = true } })
                @Html.ValidationMessageFor(model => model.ClientId, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.AddressLine1, htmlAttributes : new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.AddressLine1, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.AddressLine1, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.AddressLine2, htmlAttributes : new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.AddressLine2, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.AddressLine2, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.City, htmlAttributes : new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" data-dismiss="modal" class="btn btn-light-grey">
                Close
            </button>
            <button type="button" class="btn btn-blue" onclick="location.href = '@Url.Action("Savetodb")';return false;">
                Save changes
            </button>
        </div>
    </div>
</div>
<!-- ends: BOOTSTRAP EXTENDED MODALS -->

我在SaveToDb方法上有一个断点,但AddressViewModel对象(addrs)总是为null。请提供任何帮助,我们将不胜感激。

如何在mvc 5方法中从引导模式popoup获取输入值

这背后的原因是您的实际表单没有发布,您只是重定向到这里,这就是为什么您的表单数据没有正确绑定模型的原因。您可以执行POST ajax调用,然后传递表单数据。