嗨,我從數據庫中選擇數據,我希望這些數據從我的控制器通過ajax查看,但它不工作。傳遞數據從控制器查看使用ajax
請幫助我嗎?
這裏是我的控制器:
[HttpGet]
public ActionResult Foo(string email_uzivatele)
{
var person = AdvertServiceLayer.Instance.SelectByEmail(email_uzivatele);
return Json(person, JsonRequestBehavior.AllowGet);
}
在這裏,我想這是我從數據庫中選擇顯示電子郵件:
<input id="email" name="email_uzivatele" type="text" class="form-control input-md">
這裏是我的ajax功能:
$(function() {
function getPerson(email_uzivatele) {
$.ajax({
url: '@Url.Action("Foo", "Home")',
type: 'GET',
dataType: 'json',
cache: false,
data: { email_uzivatele: email_uzivatele },
success: function (person) {
$('#email').val(person.email_uzivatele);
}
});
}
});
這裏是功能從數據庫中選擇電子郵件:
public List<Advert> SelectByEmail(string email_uzivatele)
{
string queryString = "SELECT distinct email_uzivatele from
Reklama
where email_uzivatele like '%" + @email_uzivatele + "%'";
// Create the Command and Parameter objects.
SqlCommand command = new SqlCommand(queryString, Connection);
command.Parameters.AddWithValue("@email_uzivatele", "");
// Open the connection in a try/catch block.
// Create and execute the DataReader, writing the result
// set to the console window.
try
{
SqlDataReader reader = command.ExecuteReader();
List<Advert> advert = new List<Advert>();
while (reader.Read())
{
Advert a = new Advert();
a.email_uzivatele = reader[0].ToString();
Console.WriteLine("email_uzivatele: " + " " + "
" + a.email_uzivatele);
advert.Add(a);
}
reader.Close();
return advert;
}
catch (Exception ex)
{
chyba.zapsat_do_souboru(ex.Message);
Console.OpenStandardOutput();
Console.WriteLine(ex);
//zalogovat chybu
return null;
}
}
檢查值是否正確。保持警報警報(人)和警報(person.email_uzivatele),看看值是否正確,只是綁定問題 –
我添加了警報沒有發生。認真的是我做錯了什麼? –
如果你的'SelectByEmail'函數返回一個列表..那麼你的ajax成功應該使用一個索引來獲得值'$('#email')。val(person [0] .email_uzivatele);' – JamieD77