Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am trying to populate CreditTobeTakenTextBox automatically after selecting a 'TeacherId' DropDownList item. I have Teacher Table in database where TeacherId is a primary key, and trying to retrieve CreditToBeTaken of a particular teacher. But it's not working. I have seen similar type of example in codeproject but they are not working for me. I think there is a problem in my ajax call. Please someone help me.

What I have tried:

Action:

public JsonResult CreditToBeTaken(int id)

{
db.Configuration.ProxyCreationEnabled = false;
string credit = "select CreditToBeTaken from Teacher where TeacherId='"+id+"'";
var creditsToBeTaken = db.Teachers.SqlQuery(credit);
return Json(creditsToBeTaken);
}

View:

@Html.LabelFor(model => model.TeacherId, "TeacherId", htmlAttributes: new { @class = "control-label col-md-2" })

@Html.DropDownList("TeacherId", new SelectList(string.Empty, "Value", "Text"), "Please select a Teacher", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.TeacherId, "", new { @class = "text-danger" })





@Html.LabelFor(model => model.CreditToBeTaken, htmlAttributes: new { @class = "control-label col-md-2" })

@Html.EditorFor(model => model.CreditToBeTaken, new { @id = "creditTobeTakenTextBox", @class = "form-control" }
@Html.ValidationMessageFor(model => model.CreditToBeTaken, "", new { @class = "text-danger" })




Script File:

<script>

$(document).ready(function () {
$("#TeacherId").change(function () {


$.ajax({
url: '@Url.Action("CreditToBeTaken")',
type: 'POST',
dataType: 'json',
data: { teacherID: $("#TeacherId").val() },
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data.success) {
// fill the Credit to be taken
$("#CreditToBeTakenTextBox").val(data.creditsToBeTaken);
}
else {
// show a message in a alert or div
alert('This Teacher ID is not valid. Try again!');
}
}
});

});

});

</script>
Posted
Updated 15-Aug-16 1:13am
Comments
Richard Deeming 15-Aug-16 11:44am    
NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

In this specific instance, you're not immediately vulnerable to SQL Injection[^], since the parameter has already been parsed as an integer. However, if you get into the habit of writing code like this, it's far too easy to forget and use this pattern with user-supplied strings, which would be vulnerable.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

First off "doesn't work" means nothing to us. Does the change event fire? Does your ajax code run? Does it call your method? Do you get errors? Does the method return data? Does your function get the data? Does the success event run? Can if find your elements? Can it find what it wants in the data?

You need to learn to debug your code and step through it so you know at what point it fails, have a look at this link which shows you how to use the browser's dev tools to help

Using the browser&#39;s dev tools to diagnose ajax problems (and other things) | The ASP.NET Forums[^]

One thing I can see immediately is that the action parameter name of "id" does not match the data you are posting to it ("teacherID"). If you google how to call an MVC action from jQuery ajax you'll find hundreds of tutorials where this is covered, please do basic research before asking a question. There might well be other issues with your code apart from that one (eg your contenttype may be wrong), but going through more examples online and using debugging should get you on your way.
 
Share this answer
 
v2
Change the id to teacherID
C#
public JsonResult CreditToBeTaken(int teacherID )

So far this is the error, rest you have to debug and find the issue. Check solution 1 for debugging.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900