在剃刀中动态附加部分视图

发布于 2024-12-11 20:53:14 字数 1427 浏览 0 评论 0原文

我试图在按钮的 onclick 事件中附加一个带有部分视图(即 tr)的表。

这是按钮:

这是操作方法

function AddRowDynamic() {
    $('#EntryTable tr:last').after('@{Html.RenderPartial("WeeklyResourceAllocationPerProject");}'); 
}

这是部分视图:

    <tr>
        @using (Html.BeginForm()) {
            @Html.ValidationSummary(true)
            <td>
                @Html.EditorFor(model => model.ProjectId)
            </td>

            <td class="AllocationEntry">
                @Html.EditorFor(model => model.Saturday)
            </td>
               ...
               ...
    }
    </tr>

但是 javascript 显示错误:

网页错误详细信息

网页错误详细信息

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; GTB7.1; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E)
Timestamp: Tue, 25 Oct 2011 04:09:19 UTC


Message: Unterminated string constant
Line: 45
Char: 39
Code: 0
URI: http://localhost:65180/

这是发生错误的页面的来源:

function AddRowDynamic() {
        $('#EntryTable tr:last').after('<tr>        ------------>Line 45
<form action="/" method="post">     <td>

知道如何解决问题?

I am trying to append a table with a partial view (which is a tr) in a onclick event of a button.

Here is the button:

<input id="AddRow" type="button" value="Add Row" onclick="AddRowDynamic();" />

Here is the action method

function AddRowDynamic() {
    $('#EntryTable tr:last').after('@{Html.RenderPartial("WeeklyResourceAllocationPerProject");}'); 
}

And here is the partial view:

    <tr>
        @using (Html.BeginForm()) {
            @Html.ValidationSummary(true)
            <td>
                @Html.EditorFor(model => model.ProjectId)
            </td>

            <td class="AllocationEntry">
                @Html.EditorFor(model => model.Saturday)
            </td>
               ...
               ...
    }
    </tr>

But the javascript is showing error:

Webpage error details

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; GTB7.1; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E)
Timestamp: Tue, 25 Oct 2011 04:09:19 UTC


Message: Unterminated string constant
Line: 45
Char: 39
Code: 0
URI: http://localhost:65180/

Here is the source of the page where the error is occurring:

function AddRowDynamic() {
        $('#EntryTable tr:last').after('<tr>        ------------>Line 45
<form action="/" method="post">     <td>

Any idea how to solve the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泡沫很甜 2024-12-18 20:53:14

您应该通过 Ajax Post 获取部分信息,而不是尝试从 PartialView 渲染它。

通过使用此方法,您的模型也将更新,您将能够在控制器中获取信息

You should get your partial information by Ajax Post, instead of trying to render it from a PartialView.

By using this method, your model will be also updated and you will be able to get the information in your controller

吃不饱 2024-12-18 20:53:14

如果您想添加行而不回调服务器,那么您需要在隐藏的 div 中呈现附加行模板。然后,您可以从隐藏的 div 中选择 html 添加到表中。更改您的视图,在底部添加以下内容:

<div display="none" id="ExtraRowHtml">
    @Html.RenderPartial("WeeklyResourceAllocationPerProject")
</div>

然后,像这样更改您的函数:

function AddRowDynamic() {    
    $('#EntryTable tr:last').after($("#ExtraRowHtml").html());
}

If you want to add rows without calling back to the server, then you need to render your additional row template in a hidden div. Then, you can select the html from your hidden div to add to your table. Change your view, adding the following at the bottom:

<div display="none" id="ExtraRowHtml">
    @Html.RenderPartial("WeeklyResourceAllocationPerProject")
</div>

Then, change your function like so:

function AddRowDynamic() {    
    $('#EntryTable tr:last').after($("#ExtraRowHtml").html());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文