Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 填充文本框后立即显示图像_C#_Ajax_Asp.net Mvc_Asp.net Mvc 5 - Fatal编程技术网

C# 填充文本框后立即显示图像

C# 填充文本框后立即显示图像,c#,ajax,asp.net-mvc,asp.net-mvc-5,C#,Ajax,Asp.net Mvc,Asp.net Mvc 5,我想在填充文本框后立即显示图像,无需单击任何按钮即可获取图片 在我看来,我是这样做的: @using (Html.BeginForm()) { <input value="" id="UserID" name="UserID" type="text" class="form-control" /> <img id="CaptchaImg" alt="Captcha" src="@Url.Action("showFoto", "loan")" style="" /

我想在填充文本框后立即显示图像,无需单击任何按钮即可获取图片

在我看来,我是这样做的:

@using (Html.BeginForm())
{
    <input value="" id="UserID" name="UserID" type="text" class="form-control" />
    <img id="CaptchaImg" alt="Captcha" src="@Url.Action("showFoto", "loan")" style="" />
}
<script language="javascript" type="text/javascript">
    $.ajax({
        type: "POST",
        url: '@Url.Action("showFoto", "loan")',
        datatype: "image/jpg",
        success: function (UserID) {
            $('#CaptchaImg').attr('src', UserID);
        }
    });
</script>
我的信息来自:
求你了,需要你的帮助。。。谢谢

更改脚本以处理文本框的
.Change
事件,并将其值传递给服务器方法(注意
Change
事件在文本框失去焦点时发生)


您的控制器方法是否具有
[HttpPost]
属性?为什么要进行POST。它应该是一个GET。那么
ViewBag.Foto=virtualPath.ToAbsolute(virtualPath)
的意义是什么?ajax调用只在您第一次加载页面时运行(此时文本框没有值)-您需要处理文本框的
.change()
事件,并在ajax调用中分配其值(您没有将任何值传递给
showFoto()
[HttpPost]
public string showFoto(string UserID)
{
   string ImageID = UserID;
   var virtualPath = string.Format("~/images/profile/{0}.jpg", ImageID);
   if (System.IO.File.Exists(Server.MapPath(virtualPath)))
   {
      return virtualPath;
   }
   return null;
}
$('#UserID').change(function() {
  $.ajax({
    type: "GET",
    url: '@Url.Action("showFoto", "loan")',
    data: { userID: $(this).val() }, // pass the value of the textbox to the method
    datatype: "image/jpg",
    success: function (UserID) {
        $('#CaptchaImg').attr('src', UserID);
    }
  });
});