本文介绍了MVC和jQuery:最好的初步实践在检索的表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些jQuery的使用Ajax回发信息给我CONTROLER进行处理

I have some JQuery that uses Ajax to send information back to my controler to be processed

我这样做是这样的:

//Define my controls
<%=Html.TextBox("PName", Model.PName, new { id = "pName" })%> ...
....
....

//Get the values from my controls
var param1= $("#pName").val();
....
....

    //Define the return URL. Is this how to send info back?
    var url = '<%= Url.Content("~/Port/SaveRowAjax") %>/?ID=' + id
                + "&param1=" + param1
                + "&param2=" + param2
                + "&param3=" + param3
                + "&param4=" + param4
                + "&param5=" + param5;

    $.ajax({
        url: url,
        success: function(html) {
            alert("Success!");
        },
    });

   //My c# code, that processes the request
    public void SaveRowAjax(string param1 ....)
    {
        ...
    }

这是与MVC做的最好方法是什么?
这似乎有点混乱,当我contructing回发到服务器的URL

Is this the best way of doing it with MVC?
It seems a bit messy when i am contructing the URL to post back to the server

推荐答案

请尝试使用 SerializeArray 了解提交表单的项目。它会中的所有的值转换为JSON对象。

Try using SerializeArray for submitting your form items. It'll box all their values into a JSON object.

var link = "/Port/SaveRowAjax";
var formData = $(":input").serializeArray();
$.post(link,formData);

这篇关于MVC和jQuery:最好的初步实践在检索的表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 23:01