使用实体框架的模态中的MVC刷新表数据

时间:2017-09-26 06:36:21

标签: asp.net-mvc entity-framework

美好的一天。如何刷新模态表数据?好吧,我有这个文本框,当用户点击显示模态弹出窗口时。弹出窗口内部是一个带有表格和搜索的局部视图。当用户搜索而不是重新加载模态中的值时,它会将我重定向到局部视图本身。我见过很多像这样的例子 - https://forums.asp.net/t/2098629.aspx?Advance+search+in+a+MVC+Popup+Window+ - 但仍然无法通过它。它基本上是一个模态弹出窗口,但在其中包含一个局部视图,一旦点击搜索,只需要重新加载表而不再需要。提前谢谢

部分查看代码:

    public ActionResult ShowTaxPayer(int? page, string searchString)
    {
        var user = (from u in db2.Payers
                    select new Taxpayer
                    {
                        ID = u.objid,
                        Firstname = u.firstname,
                        Lastname = u.lastname,
                        Middlename = u.middlename,
                        Address = u.primaryaddress
                    });

        if (string.IsNullOrEmpty(searchString))
        {
           return View(user.ToList().ToPagedList(page ?? 1, 10));

        }
        else
        {
            return View(user.Where(s => s.Firstname.Contains(searchString)).ToList().ToPagedList(page ?? 1, 10));
        }
    }

剃刀:

@using (Html.BeginForm("Index", "Wizard", FormMethod.Post))
{
@Html.ValidationSummary(true)
<fieldset>
    <div class="wizard-step">

        @Html.Label("Taxpayer Name")
        @Html.TextBoxFor(m => m.taxpayername, new { data_toggle = "modal", data_target = "#myModal", data_backdrop = "static", data_keyboard = "false" })
        @Html.ValidationMessageFor(m => m.taxpayername)

    </div>

    <div class="wizard-step">

        @Html.Label("Taxpayer Address")
        @Html.EditorFor(m => m.taxpayeraddress)
        @Html.ValidationMessageFor(m => m.taxpayeraddress)

    </div>

    <div class="wizard-step">

        @Html.Label("Trade Name")
        @Html.EditorFor(m => m.tradename)
        @Html.ValidationMessageFor(m => m.tradename)
    </div>

    <div class="wizard-step confirm">

    </div>

    <p>
        <input type="button" id="back-step" name="back-step" value="<-- Back" />
        <input type="button" id="next-step" name="next-step" value="Next -->" />
    </p>
</fieldset>
}

模态:

<div class="modal fade" id="myModal" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
            <div class="modal-body">
                @{Html.RenderAction("ShowTaxPayer", "Wizard");}

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

我只需要在模态中加载模型中的数据,而不是将我重定向到实际视图。提前致谢。

1 个答案:

答案 0 :(得分:0)

这里是模型弹出

<div class="modal-body">
@{Html.RenderAction("ShowTaxPayer", "Wizard");}
</div>

并改为

  <div class="modal-body" id="popupTableName">

  </div>

并添加Javascript

     <script>
        window.onload = LoadTable();

        function LoadTable(){
             $.get( '@Url.Action("actionName","ControllerName", new { id = Model.ID } )', function(data) {
        $('#popupTableName').html(data);
    }); 
        }

function onChange(){
LoadTable();
}
        </script>