ビュー (UserPicture.aspx) には、ユーザーのドロップダウン リスト、画像、ファイルのアップロード、送信ボタンがあります。選択したユーザーを変更すると、表示される画像を対応するプロフィール写真に変更したい。ファイルのアップロードと送信ボタンを使用して、ユーザーの画像を変更するまで、すべてが正常に機能します。その後、変更されたユーザーを再選択しようとすると、JavaScript 関数はコントローラーに対して $.get(...) を起動しなくなります。変更されていないすべてのユーザーは、引き続き $get() を起動します...
どうすればこれを修正できますか?
ありがとう。
私のビュー項目:
<%= Html.DropDownList("cbUsers", (IEnumerable<SelectListItem>)@ViewBag.cbUsers, new Dictionary<string, object> { { "onChange", "swapImage()" } })%>
<input type="file" name="myImage" id="myImage"/>
<div id="ProfilePicture">
<img alt="Profile picture" src="../../images/img_profile.png" />
</div>
<button type="submit"><asp:Label runat="server" Text="Update" /></button>
画像パスを取得する方法:
function swapImage() {
var e = document.getElementById("cbUsers");
var strUser = e.options[e.selectedIndex].value;
$.get("/Admin/GetUserProfilePath",
{ val1: strUser },
function (data) {
document.getElementById("ProfilePicture").innerHTML = "<img src='" + data + "' alt='Profile picture' class='searchPicture'/>";
});
};
コントローラーでこの機能を使用すると、画像パスが返されます。
public ActionResult GetUserProfilePath(string val1)
送信ボタンはこれをトリガーします:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UserPicture(object cbUsers, HttpPostedFileBase myImage)
{
HandleUploadedFile(cbUsers, myImage);
ViewBag.cbUsers = new IEnumerable<SelectListItem>();//Get the users list
return View();
}