从视图到控制器操作方法的参数在提交时变为空 [英] Parameter from view to controller action method is becoming null on submit

查看:103
本文介绍了从视图到控制器操作方法的参数在提交时变为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个按钮的表单,一个是搜索结果,另一个是将数据导出到excel,这里我的问题是我只能在提交按钮上传递表单数据点击搜索结果,所以我将按钮设置为提交以搜索结果,并且在导出数据时,我也使用url.action方法请求具有一个参数的相同操作方法,但参数变为null,因此当我单击导出时我无法创建条件按钮,如何解决这个问题





这里是我的代码,使用按钮向动作方法发送请求

I have a form with two buttons, one is to search the results and other one is to export the data to excel, here my problem is I am able to pass form data only on submit button click to search the results, so I made button as submit to search the results, and when exporting data also i am requesting same action method with one more parameter with url.action method, but the parameter is becoming null so i am not able make a condition when i click on export button,how to resolve this problem


here is my code to send request to the action method using buttons

 @using (Html.BeginForm("Search", "Provider", FormMethod.Post, new { id = "search-form", @class = "form-horizontal" }))
                            {
<input class="btn btn-success" id="Sub" data-type="submit" type="button" value=search/>

<a href='@Url.Action("Search", "Provider", new {p1="ExportExcel" })'><input type="submit" data-type="submit" value="ExportExcel" class="btn btn-success" /></a>
}




这里将p1值传递给控制器​​我得p1为空







提前致谢



我的尝试:



我尝试过jquery也传递参数但是它也变成了null



here when passing p1 value to controller i am getting p1 as null



Thanks in advance

What I have tried:

I have tried with jquery also to pass parameter but it's also becoming null

推荐答案

你没有得到null值,因为它正在提交表单并且没有参数(p1附上这个。所以它可以添加到表格标签中,按照以下步骤:



Step1 :控制器动作

You are not getting null value because, it is submitting the form and there is no parameter(p1) attached with this. So it can be added in form tag, follow below steps:

Step1 : Controller action
[HttpPost]
public ActionResult Search(string action)
{
        if(action == "search") // Search button click
        {
	     // Implement your logic
        }
        else if(action == "ExportExcel") // Export button click
        {
	     // Implement your logic
        }
	return View("Index");
}



Step2 :查看页面


Step2 : View page

@using (Html.BeginForm("Search", "Home",
      new { id = "search-form1", @class = "form-horizontal" }, FormMethod.Post))
{
    <input class="btn btn-success" name="action" id="Sub" data-type="submit" type="button" value="search" />
 
    <a href="@Url.Action("Search", "Home")">
        <input type="submit" data-type="submit" name="action" value="ExportExcel" class="btn btn-success" /></a>
}


这篇关于从视图到控制器操作方法的参数在提交时变为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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