MVC和JQuery:检索表单数据的最佳实践 [英] MVC and JQuery: Best practice for retrieving form data

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

问题描述

我有一些使用Ajax将信息发送回控制器进行处理的JQuery

I have some JQuery that uses Ajax to send information back to my controller 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做到这一点的最好方法吗?
当我构造要发布回服务器的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:检索表单数据的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆