0

在一个视图(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();
}
4

1 回答 1

1

可能是缓存问题。尝试将此属性添加到您的 GetUserProfilePath() 操作:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

或更改.get().ajax({ type: 'get' })cache:false

于 2013-03-28T21:01:31.063 回答