从 HTML 下拉菜单中获取 C# 变量值,将参数传递给控制器对象中的变量



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

首页控制器.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

你可以试一试这样的事情吗?

我参考了一些代码部分的这些现有答案:html 选择(下拉列表(控制选定的索引更改事件 asp.net 和下拉列表选定的索引更改事件未启动

.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

祝你好运!

最好的方法是在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);
}
});

主控制器中的 C# 函数

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

最新更新