2012-03-28 37 views
0

這是我的控制器動作看起來像更新的WebGrid在下拉列表回傳asp.net的MVC

[HttpPost] 
[OutputCache(Location = OutputCacheLocation.None)] 
public ActionResult GetData(string Key, string Physician) 
{ 
    string physicianCode = Physician.Split(' ').ElementAt(0).Trim(); 
    CaseProfile caseProfile = HttpContext.Cache.Get(Key) as CaseProfile; 
    return PartialView("_CaseProfile", caseProfile); 
} 

這是我在這裏面jQuery的對話框中顯示我的部分觀點_CaseProfile

<div id="paged-case-profile-div">  
@using (Ajax.BeginForm("GetData", "Group", 
    new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "paged-case-profile-div" 
     , OnSuccess="OnSuccess", OnComplete="OnComplete", InsertionMode=InsertionMode.Replace })) 
     { 
     @Html.HiddenFor(m => m.Key) 
     @Html.DropDownList("Physician", Model.PhysicianSelectList, 
       new { style = "font-size: 11px;", onchange = "this.form.submit();" }) 
<div> 

@{ 

var grid = new WebGrid(source: Model.Items.AsEnumerable(), 
canPage: true, canSort: false, rowsPerPage: 10); 
       } 

       @grid.GetHtml(
       htmlAttributes: new { align = "center" }, 
       tableStyle: "table", 
       headerStyle: "header-row", 
       alternatingRowStyle: "alternate-row", 
       columns: 
        physicianGrid.Columns(
         physicianGrid.Column("PatientId", "Patient Id"), 
         physicianGrid.Column("MedRecNum", "Med Rec Num"), 
         physicianGrid.Column("AdmissionDate", "Admission Date"))) 
      </div> 
     } 
    </div> 

我所有的js文件都添加到_Layout.cshtml文件中。 我想要做回ajax回落在下拉列表中,並用控制器操作返回的最新內容更新paged-case-profile-div。 問題是,從服務器返回的新內容被渲染到不是我想要的新頁面。我只想讓我的div在ajax表單之外更新爲新內容。 請注意,我只是在OnSuccess和OnComplete javascript方法上顯示警告框。 您能否讓我知道我的代碼中有什麼問題?

另外 - 請注意,我已經檢查了幾乎所有關於此stackoverflow的相關問題,但他們都沒有幫助我。如果有人能指出我代碼中的錯誤,並給我建議改正它而不是指向其他問題/發佈,我將不勝感激。

回答

0

確保已引用 jquery.unobtrusive-ajax.js腳本到您的網頁或 Ajax.BeginForm助手不會比標準 Html.BeginForm幫手任何不同:

<script type="text/javascript" src="@rl.Content("~/scripts/jquery.unobtrusive-ajax.js")"></script> 

在您的下拉列表中嘗試更換:

onchange="this.form.submit();" 

與:

onchange="$(this).closest('form').submit()" 

或給你的Ajax形成一個ID,然後:當你做this.form.submit();要調用的提交底層DOM元素的事件,不執行任何的onsubmit處理程序已經

onchange="$('#myajaxform').submit()" 

訂閱此表格。

+0

它已經在我的_Layout.cshtml頁面。而我的所有其他對話框有一個Ajax形式工作正常,問題只是這部分。 – user979189 2012-03-28 16:01:27

+0

@ user979189,好的,我已經用另一個建議更新了我的答案。 – 2012-03-28 16:13:43

+0

太棒了!它像一個魅力。 – user979189 2012-03-28 16:19:29