简体   繁体   English

表单数据没有进入操作方法MVC和jquery ajax

[英]Form data not getting in action method MVC and jquery ajax

I am showing a report in my application. 我正在申请中显示一份报告。 User have facility to filter the report. 用户可以过滤报告。 So they input filter data and click 'Go' button. 因此,他们输入过滤数据并单击“开始”按钮。 On 'Go' click I use jQuery to pass the whole form to the controller action. 在'Go'上单击我使用jQuery将整个表单传递给控制器​​操作。 I want to read data from database, based on values of controls in form collection and pass the data back to the form and need to reload the report with the new data. 我想根据表单集合中的控件值读取数据库中的数据,并将数据传递回表单,并需要使用新数据重新加载报表。

I tried the following code. 我尝试了以下代码。

.cshtml .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 controller.cs

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

I tried below code too. 我也试过下面的代码。 But the parameter is empty 但参数为空

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

I am getting the call in action method. 我正在接受呼叫行动方法。 But here the parameter is in some serialised form. 但这里的参数是一些序列化的形式。 But cannot deserialise it back. 但无法将其反序列化。 How can I deserialise it to form collection of some other form.? 如何将其反序列化以形成其他形式的集合。 All I want is getting the value of input controls in the form. 我想要的只是获取表单中输入控件的值。

I tried the below two codes for deserialise. 我尝试了以下两个代码进行反序列化。 but none of them is working fine. 但他们都没有正常工作。 Only getting exception. 只有异常。

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


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

The .serialize() method will serialize the form contents to application/x-www-form-urlencoded encoding. .serialize()方法将表单内容序列化为application/x-www-form-urlencoded编码。 So this is exactly the same as if you never used jQuery and directly submited the form contents to the server. 所以这就像你从未使用过jQuery并直接将表单内容提交给服务器一样。

So you should do exactly the same if you weren't using jQuery at all -> use the view model to which this form will be bound: 如果您根本不使用jQuery,那么您应该完全相同 - >使用此表单将绑定到的视图模型:

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

And assuming that $("#trialReportForm").serialize() returned the following: 假设$("#trialReportForm").serialize()返回以下内容:

foo=bar&baz=bazinga

Here's how the model might look like: 以下是模型的外观:

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

try HTML: 试试HTML:

Inserir o Ficheiro: Descrição: Local: Inserir o Ficheiro:Descrição:Local:

In JS: 在JS中:

function inserirficheiro() { function inserirficheiro(){

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

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

In MVC 在MVC中

  [HttpPost] public JsonResult NovoFicheiro(int id, string desc, string local, HttpPostedFileBase FileUpload) { // See de values.... return Json(new { ok = true, message = "", }, JsonRequestBehavior.AllowGet); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM