加载模态框作为部分视图,仅使用asp.net。

huangapple go评论65阅读模式
英文:

Load modal as partial view with asp.net only

问题

以下是您要求的翻译内容:

我是一个刚开始使用asp.net和web开发的新手,我想知道是否有任何方法仅使用asp.net控制器来加载'bootstrap modal'?

### 例如:
部分视图HTML,命名为/View/Project/Edit.cshtml:
```html
@model Project

<div class="modal fade" id="editProject" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5" id="editHeader"></h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form asp-controller="Project" asp-action="Edit" method="post">
                <div class="modal-body">
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                    <div class="row">
                        <div class="col-sm-6">
                            <div class="form-floating mb-3 mt-3">
                                <input asp-for="Code" type="text" class="form-control" placeholder="Project code">
                                <label asp-for="Code">Project code</label>
                                <span asp-validation-for="Code" class="text-danger"></span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-success">Update</button>
                </div>
            </form>
        </div>
    </div>
</div>

Index视图HTML,命名为/View/Project/Index.cshtml:

... 表格从这里开始 ...
<tbody class="table-group-divider">
    @foreach (Project project in Model)
    {
        <tr>
            <td>@project.Description</td>
            <td>@project.Code</td>
            <td>@project.CellAgeCode</td>
            <td>@project.Current</td>
            <td>@project.Time</td>
            <td>@project.Series</td>
            <td>@project.Parallel</td>
            <td>@project.CurrentQC</td>
            <td>@project.TimeQC</td>
            <td>@project.OcvMin.Value</td>
            <td>@project.CcvMin.Value</td>
            <td>@project.OcvQC</td>
            <td>@project.CcvQC</td>
            <td>@project.OcvMax.Value</td>
            <td>@project.CcvMax.Value</td>
            <td>@project.Factory</td>
            <td>
                <div class="btn-group btn-group-sm" role="group">
                    <a asp-controller="Project" asp-action="Edit" asp-route-id="@project.Id" class="btn-group-sm btn-outline-primary">
                        <i class="bi bi-pencil-square"></i>
                    </a>
                    <a asp-controller="Project" asp-action="Delete" asp-route-id="@project.Id" class="btn-group-sm btn-outline-danger">
                        <i class="bi bi-trash3"></i>
                    </a>
                </div>
            </td>
        </tr>
    }
</tbody>
... 表格结束 ...

以及在单击表格内<a>标签时触发的控制器代码:

[HttpGet]
public async Task<IActionResult> Edit(int id)
{
    IEnumerable<Project> projects = await Getter.GetProjects();
    Project? project = projects?.FirstOrDefault(x => x.Id == id);
    if (project == null)
    {
        return View("Index");
    }
    else
    {
        return PartialView("Edit", project);
    }
}

是否有方法使模态框显示为弹出窗口,而无需任何JS或JQuery?谢谢 加载模态框作为部分视图,仅使用asp.net。


<details>
<summary>英文:</summary>

I&#39;m a new in asp.net and web in general and I would like to know if there is any way to load `bootstrap modal` using `asp.net` controllers only?

### For example:
partial view html named /View/Project/Edit.cshtml:
```html
@model Project

&lt;div class=&quot;modal fade&quot; id=&quot;editProject&quot; tabindex=&quot;-1&quot; aria-labelledby=&quot;exampleModalLabel&quot; aria-hidden=&quot;true&quot;&gt;
    &lt;div class=&quot;modal-dialog modal-lg modal-dialog-centered&quot;&gt;
        &lt;div class=&quot;modal-content&quot;&gt;
            &lt;div class=&quot;modal-header&quot;&gt;
                &lt;h1 class=&quot;modal-title fs-5&quot; id=&quot;editHeader&quot;&gt;&lt;/h1&gt;
                &lt;button type=&quot;button&quot; class=&quot;btn-close&quot; data-bs-dismiss=&quot;modal&quot; aria-label=&quot;Close&quot;&gt;&lt;/button&gt;
            &lt;/div&gt;
            &lt;form asp-controller=&quot;Project&quot; asp-action=&quot;Edit&quot; method=&quot;post&quot;&gt;
                &lt;div class=&quot;modal-body&quot;&gt;
                    &lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
                    &lt;div class=&quot;row&quot;&gt;
                        &lt;div class=&quot;col-sm-6&quot;&gt;
                            &lt;div class=&quot;form-floating mb-3 mt-3&quot;&gt;
                                &lt;input asp-for=&quot;Code&quot; type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;Project code&quot;&gt;
                                &lt;label asp-for=&quot;Code&quot;&gt;Project code&lt;/label&gt;
                                &lt;span asp-validation-for=&quot;Code&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class=&quot;modal-footer&quot;&gt;
                    &lt;button type=&quot;button&quot; class=&quot;btn btn-secondary&quot; data-bs-dismiss=&quot;modal&quot;&gt;Close&lt;/button&gt;
                    &lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot;&gt;Update&lt;/button&gt;
                &lt;/div&gt;
            &lt;/form&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

Index html named /View/Project/Index.cshtml:

... table start here ...
&lt;tbody class=&quot;table-group-divider&quot;&gt;
    @foreach (Project project in Model)
    {
        &lt;tr&gt;
            &lt;td&gt;@project.Description&lt;/td&gt;
            &lt;td&gt;@project.Code&lt;/td&gt;
            &lt;td&gt;@project.CellAgeCode&lt;/td&gt;
            &lt;td&gt;@project.Current&lt;/td&gt;
            &lt;td&gt;@project.Time&lt;/td&gt;
            &lt;td&gt;@project.Series&lt;/td&gt;
            &lt;td&gt;@project.Parallel&lt;/td&gt;
            &lt;td&gt;@project.CurrentQC&lt;/td&gt;
            &lt;td&gt;@project.TimeQC&lt;/td&gt;
            &lt;td&gt;@project.OcvMin.Value&lt;/td&gt;
            &lt;td&gt;@project.CcvMin.Value&lt;/td&gt;
            &lt;td&gt;@project.OcvQC&lt;/td&gt;
            &lt;td&gt;@project.CcvQC&lt;/td&gt;
            &lt;td&gt;@project.OcvMax.Value&lt;/td&gt;
            &lt;td&gt;@project.CcvMax.Value&lt;/td&gt;
            &lt;td&gt;@project.Factory&lt;/td&gt;
            &lt;td&gt;
                &lt;div class=&quot;btn-group btn-group-sm&quot; role=&quot;group&quot;&gt;
                    &lt;a asp-controller=&quot;Project&quot; asp-action=&quot;Edit&quot; asp-route-id=&quot;@project.Id&quot; class=&quot;btn-group-sm btn-outline-primary&quot;&gt;
                        &lt;i class=&quot;bi bi-pencil-square&quot;&gt;&lt;/i&gt;
                    &lt;/a&gt;
                    &lt;a asp-controller=&quot;Project&quot; asp-action=&quot;Delete&quot; asp-route-id=&quot;@project.Id&quot; class=&quot;btn-group-sm btn-outline-danger&quot;&gt;
                        &lt;i class=&quot;bi bi-trash3&quot;&gt;&lt;/i&gt;
                    &lt;/a&gt;
                &lt;/div&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    }
&lt;/tbody&gt;
... end of table ...

And the code from the controller that fire when click on the &lt;a&gt; tag inside the table:

[HttpGet]
public async Task&lt;IActionResult&gt; Edit(int id)
{
    IEnumerable&lt;Project&gt; projects = await Getter.GetProjects();
    Project? project = projects?.FirstOrDefault(x =&gt; x.Id == id);
    if (project == null)
    {
        return View(&quot;Index&quot;);
    }
    else
    {
        return PartialView(&quot;Edit&quot;, project);
    }
}

Is there any why to make the modal to show as popup without any JS or JQuery?
Thanks 加载模态框作为部分视图,仅使用asp.net。

答案1

得分: 0

你可以在主视图中使用Ajax渲染你的部分视图。

英文:

No..I dont think so..you can render your Partial View with ajax in your main view

答案2

得分: 0

Here is the translated code portion:

例如看看这个

$.ajax({
    type: "POST",
    data: {
        任何你需要的东西
    },
    url: '....',
    success: function (response) {
        //这是你想要注入部分内容的模态框
        $('#bodyModal').html(response);
        $('#sdpModal').modal('show');
    },
    error: function () {
        alert("发生了错误!!!");
    }
});
英文:

For example look at this

        $.ajax({
        type: &quot;POST&quot;,
        data: {
           AnyThing you need
        },
        url: &#39;....&#39;,
        success: function (response) {
        //This is your modal that you want to inject your partial in it
            $(&#39;#bodyModal&#39;).html(response);
            $(&#39;#sdpModal&#39;).modal(&#39;show&#39;);
        },
        error: function () {
            alert(&quot;An error has occured!!!&quot;);
        }

    });

答案3

得分: 0

感谢大家的帮助,我必须在 site.js 中实现以下代码:

const buttons = 'button[data-bs-toggle="win-modal"]';

$(function () {
    $(document).on('click', buttons, function () {
        var url = $(this).data('url');
        var decodeUrl = decodeURIComponent(url);
        $.get(decodeUrl).done(function (data) {
            PlaceHolder.html(data);
            PlaceHolder.find('.modal').modal('show');
        })
    });
});
英文:

Thanks for everyone for the help, I had to implement the following code in the site.js:

const buttons = &#39;button[data-bs-toggle=&quot;win-modal&quot;]&#39;;

$(function () {
    $(document).on(&#39;click&#39;, buttons, function () {
        var url = $(this).data(&#39;url&#39;);
        var decodeUrl = decodeURIComponent(url);
        $.get(decodeUrl).done(function (data) {
            PlaceHolder.html(data);
            PlaceHolder.find(&#39;.modal&#39;).modal(&#39;show&#39;);
        })
    });
});

huangapple
  • 本文由 发表于 2023年6月6日 16:30:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定