PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, September 9, 2022

[FIXED] How to return a text from Controller to View using ajax?

 September 09, 2022     ajax, asp.net-mvc, jquery     No comments   

Issue

I'm trying to return the content from the controller to the view but for some reason, it doesn't work. I want the text from the "return Contect (....)" to replace the label. Here is my code from the controller:

namespace Assignment.Controllers
{
    public class Q2Controller : Controller
    {
        // GET: Q2
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        public ActionResult ValidateInput(string myInput)
        {
 
            string temp = "";
            for (int i = myInput.Length - 1; i >= 0; i--)
            {
                temp += myInput[i].ToString();
            }
            if (temp == myInput)
               return Content("The string is palindrome");
            else
                return Content("The string is not palindrome");
        }
    }
}

And here is the View:

<body>
    <p>Please enter an alphanumeric string:</p>
    <div class="lbl">
        @Html.Label("lblpalindrome", "Is it palidrome?")
    </div>
    <div class="content">
        @Html.TextBox("myInput");
        <input id="btn1" type="button" value="Enter" onclick="ValidateInput()" />
   </div>
</body>

<script>
    function ValidateInput() {
        var result="";
        $.ajax({
            url: "@Url.Action("ValidateInput", "Q2")",
            type: "POST",
            cache: false,
            async: true,
            data: { myInput: $('#myInput').val() },
            success: function (response.data) {
                $("#lblpalindrome").text(response.data);
            },
            error: function (response.data) {
                alert("An error occurred: " + response.data);
            }

        });
    }
</script>

Solution

You also can do this way without server side call :

function ValidateInput()
{
   $("#lblpalindrome").text("Is it palidrome?");
  var input = $('#myInput').val();
  if(input!==null && input!==undefined && input!=='')
    {
      var reverseinput = input.split("").reverse().join("");
      
      if(input==reverseinput)
        {        
            $("#lblpalindrome").text("The string is palindrome");
        }
      else
        {
            $("#lblpalindrome").text("The string is not palindrome");
        }
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
    <p>Please enter an alphanumeric string:</p>
    <div class="lbl">
      <label id="lblpalindrome">Is it palidrome?</label>
    </div>
    <div class="content">
        <input type='text' id=myInput>
        <input id="btn1" type="button" value="Enter" onclick="ValidateInput()" />
   </div>
</body>



Answered By - kgajjar20
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing