通过jQuery在ASP.NET MVC提交表单时传递参数 [英] Passing parameters when submitting a form via jQuery in ASP.NET MVC

查看:81
本文介绍了通过jQuery在ASP.NET MVC提交表单时传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个表单提交通过jQuery的阿贾克斯我的控制器。下面code工作的大部分,但是,的ThreadId参数不会被通过。如果我直接调用控制器不使用jQuery,它就会通过,但使用jQuery的时候,我没有看到的ThreadId form.serialize()之后。什么会传递参数(如的ThreadId)jQuery的形式后最简单的方法?

ASPX

 <%Html.BeginForm(AddComment,家,新{=的ThreadId} Model.Id,
   FormMethod.Post,新{@id =AddComment+ Model.Id.ToString()
   @onsubmit =JavaScript的:AddComment(本);返回false}); %GT;
<%:Html.TextBox(CommentText,,新{@class =注释的文本框})%GT;
<输入ID =注释类型=提交名称=提交按钮值=发表评论/>
&所述;%Html.EndForm(); %GT;

JavaScript的

  AddComment =功能(发送){
    变种形式= $(寄件人);
    VAR数据= form.serialize();
    $阿贾克斯({
        键入:POST,
        网址:/首页/ AddComment
        数据:数据,
        数据类型:HTML,
        成功:函数(响应){
            警报(响应);
        },
        错误:功能(错误){
            警报(错误);
        }
    });
    返回false;
};

控制器

  [HttpPost]
        公众的ActionResult AddComment(字符串提交按钮,评论评论)
        {
            comment.CreatedDate = DateTime.Now;
            comment.PosterId = LoggedInUser.Id;            _repository.AddComment(注解);
            _repository.Save();            如果(Request.IsAjaxRequest())
            {
                返回视图(注释,评论);
            }
            其他
                返回RedirectToAction(「指数」);
        }


解决方案

的ThreadId 参数包含在动作表格属性。当你ajaxifying这种形式的您要发布帖子 /主页/ AddComment ,并且不再提供此参数。你可以做到以下几点,以ajaxify吧:

  $('#idofyourform')。提交(函数(){
    $阿贾克斯({
        //使用方法,在&lt定义,形式方法=POST...
        类型:this.method,        //使用行为作为中所定义,表单动作=?/主页/ AddComment的ThreadId = 123
        网址:this.action,        数据:$(本).serialize()
        数据类型:HTML,
        成功:函数(响应){
            警报(响应);
        },
        错误:功能(错误){
            警报(错误);
        }
    });
    返回false;
});

另一种可能性是在表中包括在的ThreadId 参数为隐藏字段,而不是如果把它的action属性。

I'm trying to do a form submit to my controller through jQuery Ajax. The following code works for the most part, however, the ThreadId parameter does not get passed. If I call the controller directly without using jQuery, it gets passed, but when using jquery, I don't see the ThreadId after form.serialize(). WHat would be the easiest way to pass parameters (like ThreadId) to jQuery form post?

ASPX

<% Html.BeginForm("AddComment", "Home", new { ThreadId = Model.Id },
   FormMethod.Post, new { @id = "AddComment" + Model.Id.ToString(),
   @onsubmit = "javascript:AddComment(this);return false" }); %>
<%: Html.TextBox("CommentText", "", new { @class = "comment-textbox" })%>
<input id="Comment" type="submit" name="submitButton" value="Post Comment" />   
<% Html.EndForm(); %>

JavaScript

    AddComment = function (sender) {
    var form = $(sender);
    var data = form.serialize();
    $.ajax({
        type: "POST",
        url: "/Home/AddComment",
        data: data,
        dataType: "html",
        success: function (response) {
            alert(response);
        },
        error: function (error) {
            alert(error);
        }
    });
    return false;
};

CONTROLLER

    [HttpPost]
        public ActionResult AddComment(string submitButton, Comment comment)
        {
            comment.CreatedDate = DateTime.Now;
            comment.PosterId = LoggedInUser.Id;

            _repository.AddComment(comment);
            _repository.Save();

            if (Request.IsAjaxRequest())
            {
                return View("Comment", comment);
            }
            else
                return RedirectToAction("Index");
        }

解决方案

The ThreadId parameter is included in the action attribute of the form. When you are ajaxifying this form you are posting to /Home/AddComment and no longer supplying this parameter. You could do the following to ajaxify it:

$('#idofyourform').submit(function() {
    $.ajax({
        // use the method as defined in the <form method="POST" ... 
        type: this.method,

        // use the action as defined in <form action="/Home/AddComment?ThreadId=123"
        url: this.action,

        data: $(this).serialize(),
        dataType: 'html',
        success: function (response) {
            alert(response);
        },
        error: function (error) {
            alert(error);
        }
    });
    return false;
});

Another possibility is to include the ThreadId parameter inside the form as hidden field instead if putting it in the action attribute.

这篇关于通过jQuery在ASP.NET MVC提交表单时传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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