【问题标题】:Asp.Net Core - How to use the amount of input in the loop with Ajax?Asp.Net Core - 如何使用 Ajax 循环中的输入量?
【发布时间】:2021-10-28 21:08:39
【问题描述】:

我在 Html 中有一个 Input 标签:

 <div class="form-group col-md-5">                           
     <input asp-for="CountSlide" class="form-control" value="2"/>                      
     <a id="add-slide" class="btn btn-info m-t-5" href="#">Add</a>
 </div>

我在 Razor 视图中有一个循环:

@for (int i = 0; i < count; i++)
       {
         <div class="form-group">                               
           <input type="file" onchange="readURL(this);" class="form-control">                                
         </div>
       }

我想在点击添加按钮时使用循环中的输入值(“count”)?

【问题讨论】:

标签: javascript html jquery ajax asp.net-core


【解决方案1】:

最新的

您可以将您的@for 代码放在部分视图中。

How to use jquery or ajax to update razor partial view in c#/asp.net for a MVC project


你想让@for (int i = 0; i &lt;count; i++)中的下面的div根据输入的值来渲染元素,这是不可能的。

只能通过javascript实现,不能使用razor引擎。

相关帖子:What is the order of execution of the C# in Razor

简单的解释是当你可以看到页面的所有内容时,后端的C#代码将不再执行。

私人

您可以使用@count 来获取值。

@{
    ViewData["Title"] = "Home Page";
    int count = 5;
}
<script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>

<script>
    function readURL(url) {
        alert(url)
    }
    function func() {
        var get_count = @count;
        alert(get_count);
        //$.ajax({
        //    url: "url",
        //    type: "POST",
        //    data: {a:"a"},
        //    success: function (veri) {
        //        console.log("success");
        //    },
        //    error: function (hata, ajaxoptions, throwerror) {
        //        alert("failed");
        //    }
        //});
    }

</script>

<div class="form-group col-md-5">
    <input  class="form-control" value="2" />
    <a id="add-slide" class="btn btn-info m-t-5" href="#"  onclick="func()">Add</a>
</div>

<hr />

@for (int i = 0; i < count; i++)
{
    <div class="form-group">
        <input type="file" onchange="readURL(this);" class="form-control">
    </div>
}

【讨论】:

  • 谢谢,但我想从 Input 中获得价值
  • 有可能我将值发送到控制器并使用 ajax 再次将其传递给查看??
  • 是的,您可以将您的@for 代码放在部分视图中。 i.imgur.com/J8nIN8t.png
  • 照片没有为我打开
猜你喜欢
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-07
  • 2020-09-12
  • 1970-01-01
相关资源
最近更新 更多