简体   繁体   English

我们可以使用Html.Partial Helper附加jQuery中的HTML(大块)吗? C#ASP.Net MVC 5

[英]Can We Append (Chunk of) HTML in jQuery using Html.Partial Helper? C# ASP.Net MVC 5

As the title suggests, I have a chunk of HTML which I put in one of my PartialView : 顾名思义,我有一部分HTML放在我的PartialView

<div id="meaningPart">
  @Html.Partial("_Meaning", new MeaningModel() {number = 1})
</div>

where my _Meaning partial View looks like this: 我的_Meaning部分View如下所示:

<div class="chunk">
   <!--a lot of html here-->
</div>

Then, I create a jQuery which respond to a <input type="number"/> event (up and down) like the following: 然后,我创建一个jQuery ,它响应<input type="number"/>事件(上下),如下所示:

<input id="numberMeaning" type="number" class = "col-md-2 form-control form-control-plus number-input" min = "0" max = "99" value = "1"/>

$(document).ready(function () {
  var iCnt = 0;

  $("#numberMeaning").on('change', function () {
    var num = parseInt($("#numberMeaning").val());
    if (num > iCnt) { //up
      iCnt++;       
      $("#meaningPart").append("<p>ok dude, this is really a new item!</p>");
    } else { //down
      iCnt--;
    }
  });
});

now, the above code works fine because I simply put "<p>ok dude, this is really a new item!</p>" in the jQuery append , but if I change this part: 现在,上面的代码可以正常工作,因为我只是在jQuery append放了"<p>ok dude, this is really a new item!</p>" ,但是如果我更改了这一部分:

$("#meaningPart").append("<p>ok dude, this is really a new item!</p>");

Into 进入

$("#meaningPart").append("@Html.Partial("_Meaning", new MeaningModel() {number = 2})"); //by right, the number should follow iCnt here.

Then, although the razor engine shows that the syntax is correct, the new Partial HTML just doesn't render. 然后,尽管剃刀引擎显示语法正确,但是新的Partial HTML无法呈现。

How do we create a new partial HTML using jQuery ? 我们如何使用jQuery创建新的部分HTML Is that possible? 那可能吗?

(I am not too familiar with template attribute or jQuery.clone - but if it anyone can show how the adding to the html element can be done using those, I am open to that too) (我对template属性或jQuery.clone不太熟悉-但如果有人可以展示如何使用这些属性来完成html元素的添加,我也很乐意)

try it like below 尝试如下

$(document).ready(function () {
  var iCnt = 0;

  $("#numberMeaning").on('change', function () {
    var num = parseInt($("#numberMeaning").val());
    if (num > iCnt) { //up
      iCnt++;       
      // your GET /Controller/Action should return "partial HTML" based on iCnt
      $("#meaningPart").append($('<div>').load("/Controller/Action?iCnt=" + iCnt));
    } else { //down
      iCnt--;
    }
  });
});

your Action in Controller like 您在控制器中的操作,例如

public ActionResult Action(int iCnt)
{
    //get model based on 'iCnt'
    var model = GetModel(iCnt);  
    return PartialView("~/views/Folder/partialview.cshtml", model);
}

You can use $('#selector').html(content) where content is your template. 您可以使用$('#selector')。html(content),其中content是您的模板。 I cannot imagine to create a partial view using jquery 我无法想象使用jQuery创建局部视图

Short answer: No. 简短答案:不可以。

Please be aware of the difference between JavaScript (Jquery) and MVC/ASP.Net (Razor). 请注意JavaScript(Jquery)和MVC / ASP.Net(Razor)之间的区别。

JavaScript is executed on the client site in the browser. JavaScript在浏览器的客户端站点上执行。 MVC is executed on the server. MVC在服务器上执行。

So the server is reading the razor stuff and creating a valid HTML page from it. 因此,服务器正在读取剃须刀中的东西,并从中创建一个有效的HTML页面。 This is delivered to the browser. 这被传送到浏览器。 The browser is creating something the user can see and then is executing the JavaScript. 浏览器正在创建用户可以看到的内容,然后执行JavaScript。

So what you are doing in your example is trying to get JavaScript to execute a Razor function. 因此,您在示例中所做的就是尝试使JavaScript执行Razor函数。

The only way to do something like that would be to use AJAX. 做到这一点的唯一方法是使用AJAX。

You can do it. 你能行的。

First you need to put the chunk of your partial html to a js var inside your main .cshtml file. 首先,您需要将部分html的一部分放入主.cshtml文件中的js var中。

<script>
    var chunkHtml = "@Html.Partial("_Meaning", new MeaningModel() {number = 2})"
</script>

and then do your append in your javascript file. 然后在您的javascript文件中添加附录。

$("#meaningPart").append(chunkHtml);

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

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