0

I Have a Input tag in Html :

 <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>

And I Have A Loop in Razor View :

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

I want to use the input value in the loop ("count") when the add button is clicked?

0

1 Answer 1

1

NEWEST

You can put your @for code in partial view.

enter image description here

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


You want the following div in @for (int i = 0; i <count; i++) to render elements according to the value in the input, which is impossible.

You only can do it by javascript, not use razor engine.

Related Post: What is the order of execution of the C# in Razor

The simple explanation is that when you can see all the contents of the page, the back-end C# code will no longer be executed.

PRIVIOUS

You can use @count to get the value.

enter image description here

@{
    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>
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks but I want to getting value from Input
it's posible I send the value to the controller and pass it to view again with ajax??
Yes, you can put your @for code in partial view. i.imgur.com/J8nIN8t.png
The photo did not open for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.