使用隐藏输入生成表单并提交给操作 [英] Generating form with hidden input and submitting to an action

查看:152
本文介绍了使用隐藏输入生成表单并提交给操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这会比以前简单得多。我需要从我的数据表中的单元格中提取数据(我可以这样做),然后通过带有隐藏字段的表单提交数据,以便将该变量传递给我的控制器中的某个动作。



我在代码中做了与之前类似的事情,除了生成表单。



我尝试了不同的方法,我在我的操作中访问集合的值为空。



我的查看功能:

 < $('#myTable')。on('click','td',function(){
var serial = table.cell(this).data();


var form = $('< form action =@ Url.Action(status)id =hiddenform>< input type =hiddenid =serialname =serialvalue =>< / form>')。appendTo('body');
$('。serial')。val(serial);
form.submit );


});

和我的控制器我解析为:

  //接受GET和POST 
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult Status(System.Web.Mvc.FormCollection集合)
{
string serial = collection [serial];

if(serial == null || serial ==)
System.Windows.Forms.MessageBox.Show(Error:Serial is NULL);

return View(serial);
}

老实说,我为了解决这个问题而屠杀了我的代码所以它看起来很糟糕。



任何帮助将不胜感激。



我收到错误:Serial is NULL在我的控制器中每次都弹出。但是,如果我测试JQuery的值不为空,它会返回正确的值。 您是否尝试将方法签名更改为以下?

  public ActionResult Status(string serial)

其中serial将是您从HTML表单传递的参数名称。



如果参数名称为blah在HTML中,您可能必须将方法签名更改为以下内容:

  public ActionResult Status(string blah)


I thought this would be a lot simpler than it had been. I need to extract data from a cell in my datatable (Which I can do) and then submit it via a form with a hidden field in order to pass the variable to an action in my controller.

I am doing something similar to this prior in my code with the exception of generating the form.

I have tried different methods and not matter what when I access the collection in my action the value is null.

My View function:

$('#myTable').on('click', 'td', function () {
            var serial = table.cell(this).data();


            var form = $('<form action="@Url.Action("status")" id="hiddenform"><input type="hidden" id="serial" name="serial" value=""></form>').appendTo('body');
            $('.serial').val(serial);
            form.submit();


        });

and my controller I am parsing to:

//Accept GET and POST
    [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
    public ActionResult Status(System.Web.Mvc.FormCollection collection)
    {
        string serial = collection["serial"];

        if (serial == null || serial == "")
            System.Windows.Forms.MessageBox.Show("Error: Serial is NULL");

        return View(serial);
    }

To be honest I have butchered my code a lot in an attempt to fix this so it looks pretty awful.

Any help would be appreciated.

I am receiving the Error: Serial is NULL popup every time in my controller. However if I test the JQuery the value is not null it returns the correct value.

解决方案

Have you tried changing the method signature to the following?

public ActionResult Status(string serial)

where serial will be the parameter name that you will pass from the HTML form.

If the parameter name is blah in HTML, you might have to change your method signature to below:

public ActionResult Status(string blah)

这篇关于使用隐藏输入生成表单并提交给操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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