【问题标题】:Grab C# variable value from a HTML dropdown-menu to pass parameter to variable in Controller Object从 HTML 下拉菜单中获取 C# 变量值以将参数传递给控制器​​对象中的变量
【发布时间】:2020-02-07 03:34:59
【问题描述】:

我有一个问题,我什至不知道如何开始。我有这个控制器:

HomeController.cs

using dependencies;

namespace Automata.Controllers
{
    public class HomeController : Controller
    {
        public System.Data.DataTable dt_PagosRecibidos = new System.Data.DataTable();
        public ActionResult PagosRecibidos()
        {
            cargarPagosRecibidos();
            return View();
        }
        public void cargarPagosRecibidos()
        {
            string myVar = "";
            String SentaiAppServer = System.Configuration.ConfigurationManager.AppSettings["SentaiAppServer"].ToString();
            string sURL = SentaiAppServer + ("app/interfaces/bbva/sp_EstadoCuenta.html?fecha_ini=07/16/19&fecha_fin=07/16/19&interfazado=no&Sucursal=" + myVar + "&");
            System.Data.DataSet ds = clsUtilerias.ObtenerDSInterfaceSentai(sURL);
            dt_PagosRecibidos = ds.Tables[1];
        }
    }
}

我也有我的 HTML 下拉列表:

<div class="col-lg-2 col-md-3 form-group pull-right">
    <label>Sucursal:</label>
    <select id="sucursalTabla" onchange="myFilter()" class="form-control">
        <option value="" selected>Filtrar..</option>
        <option value="MY">Monterrey</option>
        <option value="GD">Guadalajara</option>
        <option value="MX">Mexico</option>
        <option value="PB">Puebla</option>
        <option value="VR">Veracruz</option>
        <option value="MD">Merida</option>
        <option value="SA">Saltillo</option>
    </select>
</div>

如何使控制器中的option value="XX" 等于myVar

或者有没有其他方法可以使用下拉菜单来更改我在控制器中的对象中的变量?

我需要对日期做同样的事情,

("app/interfaces/bbva/sp_EstadoCuenta.html?fecha_ini=07/16/19&fecha_fin=07/16/19&interfazado=no&Sucursal=" + myVar + "&");

fecha_ini=07/16/19
fecha_fin=07/16/19

这些也可能是变量,例如

starte_date = picker.startdate
end_date = picker.enddate

【问题讨论】:

  • 既然您显然在使用 ASP.NET MVC,为什么不直接使用 Html.DropDownListFor?见How to write a simple Html.DropDownListFor()?
  • 函数myFilter在哪里?
  • @EGC 没有 myFilter 功能,对于复制和粘贴,您有什么建议吗?

标签: c# html asp.net datatable dropdown


【解决方案1】:

最好的方法是在 javascript 函数中设置一个 ajax 调用,并通过端点将您需要的数据发送到您的控制器。所以基本上是这样的:

Javascript:

$.ajax({
   type: "POST",
   url: "Home/GetInfo", //The controller class name (minus 'Controller') + '/' + Function Name
   data: {
      var: optionValue, //var being the name of the variable in the c# function and optionValue being the value of the dropdown
      date: date
   },
   success: function () {
      // Do whatever you want when the function returns successfully
      alert("Successfully sent data")
   },
   error: function (xhr, status, error) {
      // If something went wrong in making the call 
      // (it couldnt find the endpoint or an exception got called in the c# function), 
      // do something with the error data
      alert(error);
   }
});

HomeController 中的 C# 函数

public void GetInfo(string var, DateTime date){
   string myVar = var;
   DateTime myDate = date;
}

【讨论】:

  • 对不起先生,但我不明白如何实现这个,你能举个例子吗?
  • 这段代码已经是一个例子。只需将该 ajax 调用放入 javascript 中的函数中,该函数会在您需要时调用,将 ajax 调用的数据部分中的变量自定义为您需要的任何变量,仅此而已。我在代码中添加了 cmets 以使其更加清晰。希望这会有所帮助!
【解决方案2】:

你可以试试这样的东西吗?

对于某些代码部分,我参考了这些现有答案:html select (dropdown) control selected index changed event in asp.net & Dropdown selected index changed event not firing up

HTML

<div class="col-lg-2 col-md-3 form-group pull-right">
    <label>Sucursal:</label>
    <select id="sucursalTabla" OnSelectedIndexChanged="ddl_SelectedIndexChanged" AutoPostBack="True" class="form-control">
        <option value="" selected>Filtrar..</option>
        <option value="MY">Monterrey</option>
        <option value="GD">Guadalajara</option>
        <option value="MX">Mexico</option>
        <option value="PB">Puebla</option>
        <option value="VR">Veracruz</option>
        <option value="MD">Merida</option>
        <option value="SA">Saltillo</option>
    </select>
</div>

C#

using dependencies;

namespace Automata.Controllers
{
    public class HomeController : Controller
    {
        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
       {
            //code here
       }

        public System.Data.DataTable dt_PagosRecibidos = new System.Data.DataTable();
        public ActionResult PagosRecibidos()
        {
            cargarPagosRecibidos();
            return View();
        }
        public void cargarPagosRecibidos()
        {
            string myVar = "";
            String SentaiAppServer = System.Configuration.ConfigurationManager.AppSettings["SentaiAppServer"].ToString();
            string sURL = SentaiAppServer + ("app/interfaces/bbva/sp_EstadoCuenta.html?fecha_ini=07/16/19&fecha_fin=07/16/19&interfazado=no&Sucursal=" + myVar + "&");
            System.Data.DataSet ds = clsUtilerias.ObtenerDSInterfaceSentai(sURL);
            dt_PagosRecibidos = ds.Tables[1];
        }
    }
}

要专门从EventArgs e值访问元素,请参考官方文档:HtmlSelect Class & DropDownList.SelectedIndex Property

祝你好运!

【讨论】:

    猜你喜欢
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    相关资源
    最近更新 更多