在剃刀中动态附加部分视图
我试图在按钮的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该通过 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
如果您想添加行而不回调服务器,那么您需要在隐藏的 div 中呈现附加行模板。然后,您可以从隐藏的 div 中选择 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:
Then, change your function like so: