简体   繁体   English

ASP.Net MVC 5:运行时Html.RenderAction(“…”)

[英]ASP.Net MVC 5: Html.RenderAction(“…”) at runtime

I want to add a PartialView multiple times by pressing a button. 我想通过按一个按钮多次添加PartialView。

<div id="FilterRows">
     @{Html.RenderAction("_FilterRow");}
</div>

<button id="newRow" type="button"
     class="btn btn-sm btn-default" 
     style="width: 50px">+</button>

This piece of code works properly. 这段代码可以正常工作。 But now i want to append the div FilterRows with another PartialView of _FilterRow at clicking on the button. 但是现在我想在单击按钮时将div FilterRows附加另一个_FilterRow的PartialView。

This is how it looks today: 今天是这样的: 在此处输入图片说明

Something like: 就像是:

$(document).ready(function() {
    $("#newRow").click(function() {
       $("#Exec").append("<br>", @{Html.RenderAction("_FilterRow");} {
       });  
    });
});

Is unfortunately not working. 不幸的是无法正常工作。 Any Ideas? 有任何想法吗?

If you add an action which returns the partial rendered as a partial (ie. return PartialView("myView", model); then you can load using jQuery: 如果添加一个将部分渲染返回为部分的动作(即, return PartialView("myView", model);则可以使用jQuery加载:

# Create a new element to contain...
var el  = $('<div></div>');
$('#parent').append(el);
# ...the new content
el.load('@Url.Action("action", "controller"');

(This means running the JS in the razor view to get the correct URL generation. If most of the JS is in its own file, pass the URL from a little JS in the Razor file just for things like URLs.) (这意味着在razor视图中运行JS以获得正确的URL生成。如果大多数JS在其自己的文件中,则仅将Razor文件中的小JS的URL传递给URL之类的东西。)

As long as your script is in the page (and not in an external .js file) you can use Razor inside js (although feedback directly from MicroSoft indicates that this is "unexpected", it works fine). 只要您的脚本在页面中(而不是在外部.js文件中),您就可以在js中使用Razor(尽管直接来自MicroSoft的反馈表明这是“意外的”,但效果很好)。

Have a look at the rendered html to see what's wrong. 看看呈现的html,看看有什么问题。

In this case you need quotes (") around the render action: 在这种情况下,您需要在渲染操作周围加上引号(“):

$("#FilterRows").append("@{Html.RenderAction("_FilterRow");}");

This assumes a number of potential issues: 这假定了许多潜在的问题:

  • the 'RenderAction' can't have any newlines in the output 'RenderAction'在输出中不能有任何换行符
  • the 'RenderAction' can't have any quotes in the output (either use ' on the append and " inside the render or the other-way-around) 该“的RenderAction”不能在输出的任何引号(或者使用'上追加和"内部渲染或另一方向的全能)
  • the action to be rendered cannot have any row-specific parameters (which appears to be ok in this case to add a new blank row) 要呈现的动作不能具有任何特定于行的参数(在这种情况下,添加新的空白行似乎可以)
  • the script must be in a .cshtml file (though you can get around this by setting a global/namespace'd variable in the .cshtml and have the actual code in a .js file) 该脚本必须位于.cshtml文件中(尽管您可以通过在.cshtml中设置全局/命名空间变量来解决此问题,并在.js文件中包含实际代码)
  • you need to use the correct combination of @{} / @() and render/tostring 您需要使用@{} / @()和render / tostring的正确组合

You might be better off with @Html.RenderPartial if you just want to render some html and don't need an action. 如果您只想呈现一些html并且不需要执行任何操作,则使用@Html.RenderPartial可能会更好。


An alternative, perhaps more friendly, mechanism would be to have the blank-row already on the page (perhaps hidden) and use .clone() . 另一种可能更友好的机制是在页面上已经有空白行(也许是隐藏的)并使用.clone()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM