【问题标题】:Render view in modal以模态渲染视图
【发布时间】:2019-06-12 12:58:29
【问题描述】:

我正在尝试以模式呈现视图,但我不知道如何做到这一点。

我通过创建局部视图尝试了一种解决方法,但无法触发主控制器中的方法(在加载表单上)。

我想知道,是否可以让部分视图触发控制器中的方法,或者我应该将其更改为临时视图(我没有设法显示它)?

查看:

@model IEnumerable<CharityProject.Models.UserInfos>

@{
    ViewData["Title"] = "SelectAddress";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Select Address</h2>

<p>
    @* <input type="button" value="Add New Address" onclick="location.href='@Url.Action("Create", "UserInfos")'" />*@
    <input type="button" data-toggle="modal" data-target="#myModal" value="Add New Address" class="btn btn-default">
</p>
<div class="row">
    <div class="col-md-6">
        <table class="table">
            <thead>
                <tr>
                    <th>
                        @Html.DisplayNameFor(model => model.Cities)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Area)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Address)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.POB)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.PhoneNo)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.PhoneNo2)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.IsDefault)
                    </th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.Cities.CityName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Area)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Address)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.POB)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.PhoneNo)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.PhoneNo2)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.IsDefault)
                        </td>
                        <td>
                            <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                            <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                            <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
    <div class="col-md-6">


    </div>
</div>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" 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">
                ...
                **@{await Html.RenderPartialAsync("CreateAddress", new UserInfos());}**
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" uiactions="@Url.Action("Create", "UserInfos")" formmethod="post">Save changes</button>
            </div>
        </div>
    </div>
</div>

家庭控制器:

public ActionResult CreateAddress()
        {
            return View();
        }

【问题讨论】:

  • 使用 Javascript 执行 ajax 请求以显示/调用服务器上的方法。

标签: c# asp.net-mvc asp.net-core modal-dialog render


【解决方案1】:

我还没有使用过,Net Core,还在学习过程中,但是从我记得的经典 MVC 方法(.NET)中,

创建一个将数据发送到控制器的 AJAX 方法

@Html.Action/@Html.ActionLink/@Url.Action等

部分视图应该可以访问主视图的模型对象(如果您执行@Html.Partial)

【讨论】:

  • 我是用 ajax 制作的,但是当我提交表单时会重定向到实际页面并关闭弹出窗口。
  • 否,因为即使模型有错误,提交时也会自动关闭弹出窗口。
  • pop关闭的原因可能是因为你使用的ajax调用以close modal语句结束,能把你使用的方法和得到的错误贴出来吗?
  • 看看这个link
【解决方案2】:

如果您使用的是 ASP.NET Core,则应使用视图组件。

创建一个派生自包含 InvokeAsync 方法的 ViewComponent 的类:

public class AddressViewComponent : ViewComponent
{
    public async Task<IViewComponentResult> InvokeAsync()
    {
        return View();
    }
}

把你的视图放在这个路径结构“/Views/Shared/Components/Address/Default.cshtml”中。

然后,在你的模式中渲染组件:

<div class="modal-body">
    @await Component.InvokeAsync("Address")
</div>

【讨论】:

  • 我在 @{ await Component.InvokeAsync("CreateAddress" } 上收到此错误。传递到 ViewDataDictionary 的模型项的类型为 'System.Collections.Generic.List`1[CharityProject.Models .UserInfos]',但此 ViewDataDictionary 实例需要“CharityProject.Models.UserInfos”类型的模型项。
  • 在 CreateAddress 组件的视图中检查模型,可能类似于 @model UserInfos 但您的 InvokeAsync 方法返回的是一个列表而不是单个对象。
猜你喜欢
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多