2013-08-27 28 views
0

我在我的應用程序中顯示一個報告。用戶可以過濾報告。所以他們輸入過濾數據並點擊'Go'按鈕。在'去'點擊我使用jQuery將整個表單傳遞給控制器​​操作。我想根據表單集合中控件的值讀取數據庫中的數據,並將數據傳回表單並需要使用新數據重新加載報表。表單數據沒有得到行動方法MVC和jquery ajax

我試過下面的代碼。

.cshtml

<script type="text/javascript"> 

$(document).ready(function() { 

    $('#action_button').click(function() { 

     //$('#trialReportForm').attr("action", '@Url.Action("ReportFilter", "MyController")');  
     //$('#trialReportForm').submit();  
// While using above two commented lines instead of the below codes, I get the form collection correctly. BUt I cannot pass back the new data to the form. If this is the solution.. How can I pass the data back to form to reload the report?       



     var formElements = $("#trialReportForm").serialize(); 

    //var data = { "parameter": $.toJSON(formElements) }; 

var data = { "parameter": formElements }; 


$.ajax({ 
    url: @Url.Action(ReportFilter", "MyController"), 
    type: 'POST', 
    data: data, 
    dataType: 'json', 
    success: OnSuccess, 
    error: OnFailure 
}); 


    }); 


function OnSuccess(result) 
{ 
alert(result); 
} 

function OnFailure(result) 
{ 
alert(result); 
} 

}); 

controller.cs

[HttpPost] 
    public JsonResult ReportFilter(string parameter) 
    { 
     return new DBConnect().GetFilterData(parameter); 
    } 

我想下面的代碼了。但參數爲空

[HttpPost] 
    public JsonResult ReportFilter(FormCollection parameter) 
    { 
     return new DBConnect().GetFilterData(parameter); 
    } 

我正在調用操作方法。但是這裏的參數是以一些序列化的形式出現的。但是不能將其反序列化。我如何反序列化它以形成其他形式的集合? 我想要的就是獲取表單中輸入控件的值。

我嘗試了以下兩個代碼進行反序列化。但他們都沒有工作正常。只有得到例外。

1: var serializer = new JavaScriptSerializer(); 
    var jsonObject = serializer.Deserialize<FormCollection>(parameter); 


2: var request = JsonConvert.DeserializeObject<FormCollection>(parameter); 

回答

2

.serialize()方法將序列化形式內容application/x-www-form-urlencoded編碼。因此,這與您從未使用過jQuery並直接將表單內容提交給服務器的方式完全相同。

所以,你應該,如果你不使用jQuery都做同樣的 - >使用此表格將被綁定視圖模型:假設$("#trialReportForm").serialize()回到

[HttpPost] 
public JsonResult ReportFilter(MyViewModel model) 
{ 
    ... 
} 

及以下:

foo=bar&baz=bazinga 

這裏的模式如何看起來像:

public class MyViewModel 
{ 
    public string Foo { get; set; } 
    public string Baz { get; set; } 
} 
0

試 HTML:

InserirØFicheiro: Descrição: 地方:

在JS:

功能inserirficheiro(){

var desc = $("#inserirficheirodescricao").val(); 
    var local = $("#inserirficheirolocal").val(); 

    var formData = new FormData(); 

    var file = document.getElementById("files").files[0]; 

    formData.append("FileUpload", file); 
    formData.append("desc", desc); 
    formData.append("local", local); 
    formData.append("id","1"); 

    $.ajax({ 
     type: "POST", 
     url: "@Url.Content("~/gestaoficheiros/NovoFicheiro/")", //+ "?desc=" + desc + "&local=" + local, 
     data: formData, 
     cache: false, 
     dataType: 'json', 
     contentType: false, 
     processData: false, 
     success: function (data) { 
      if (data.ok == true) { 

      } 
      else { 

       return; 
      } 
     }, 
     error: function (error) { 
      if (error.ok == undefined) { 
       alert("Sessão Expirou"); 
       location.href = '@Url.Content("~")'; 
       return; 
      } 
      alert("!Erro, resposta do Servidor: " + error.responseText); 
     } 
    }); 

} 

在MVC

[HttpPost] 
    public JsonResult NovoFicheiro(int id, string desc, string local, HttpPostedFileBase FileUpload) 
    { 
     // See de values.... 

     return Json(new { ok = true, message = "", }, JsonRequestBehavior.AllowGet); 
    } 
相關問題