2013-03-27 157 views
0

在視圖(UserPicture.aspx)中,我有一個用戶下載列表,一個圖像,一個文件上傳和一個提交按鈕。當我更改選定的用戶時,我想將顯示的圖像更改爲相應的個人資料圖片。每一件事情都很好,直到我修改用戶的圖像,使用fileupload並提交按鈕。之後,如果我嘗試重新選擇更改的用戶,則javascript函數不會將$ .get(...)發送給控制器。所有未更改的用戶仍然觸發$ get()...mvc dropdownlist選擇的項目不能重新選擇

我該如何解決這個問題?

謝謝。


我查看項目

<%= 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'/>"; 
    }); 
}; 

有了這個finction控制器返回圖像路徑:

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(); 
} 

回答

1

可能是一個緩存問題。嘗試添加該屬性到您GetUserProfilePath()動作:

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

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

+0

緩存:假的伎倆。謝謝 – GaZ 2013-03-28 21:40:22

+0

其他解決方案的工作原理是在原代碼中使用POST代替GET。 – GaZ 2013-03-28 21:44:12