Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8185381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:49:44+00:00 2026-06-07T01:49:44+00:00

I am trying a simple task of displaying student name in a textbox based

  • 0

I am trying a simple task of displaying student name in a textbox based on StudentId entered. I am able to display the student name as an alert from jQuery – AJAX call but not in the text box, what am I missing here?

Controller:

    [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
    public ActionResult DisplayStudentName(string id)
    {
        StudentDataContext db = new StudentDataContext();
        var StudentName = (from p in db.vwStudents.Where(a => a.StudentNumber == id)
                         group p by p.StudentName into g
                         select g.Key).FirstOrDefault();

        return Json(new { Name = StudentName }); 

    }

jQuery:

$(function () {
    $('#submitButton').click(function () {
        var link = '/StudentForm/DisplayStudentName';        
        $.ajax({
            type: 'POST',
            url: link,
            data: { id: $('#id').val() },
            dataType: 'json',
            success: function (result) {
                $("#StudentName").val(result.Name);
                alert(result.Name);
            },
            error: function (result) {
                alert("Failed")
            }
        });
    });
});

View:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Student Form
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="Data" style="text-align: left; height: 202px;">
Student Number:<input type="text" name="id" id="id"/><br />                
Student Name:<input type="text" name="StudentName" id="StudentName"/><br />              
<br />
 <div id="Div1">
  <button id="submitButton" name="submitButton" style="width:140px;">Display Short Name</button>
 </div>
</div>

</asp:Content>

Again, I am able to display Student Name in the Alert window, but not in the text box, Am I missing something?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:49:46+00:00Added an answer on June 7, 2026 at 1:49 am

    You need to prevent the default behavior of submit button. You can use the jQuery preventDefault function to do this,

    $(function () {
        $('#submitButton').click(function (e) {
            e.preventDefault();    //prevent default behaviour
            var link = '/StudentForm/DisplayStudentName';        
            $.ajax({
                type: 'POST',
                url: link,
                data: { id: $('#id').val() },
                dataType: 'json',
                success: function (result) {                  
                    $("#StudentName").val(result.Name);
                },
                error: function (result) {
                    alert("Failed")
                }
            });
        });
    })
    

    When preventDefault method is called, the default action of the event will not be triggered. So in this case the form submission will not happen ( so the page wont be reloaded).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do a simple task with jQuery: I have a list of
I'm trying to do a simple task of reading space separated numbers from console
I'm trying to accomplish one simple task (but is not simple for me). I
I'm trying to accomplish a simple task of creating a database with 2 tables
I have a very simple task I am trying to do in Groovy but
I'm trying to accomplish what should be a very simple task using the ListView
Currently I'm trying to do what I thought would be a simple task: Draw
I am trying this simple tutorial from oracle : http://www.oracle.com/technetwork/java/socket-140484.html (the Example 1 ).
I'm trying to create a simple app, whose main task is to open the
Trying to do the simple task of writing to the registry to make a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.