【问题标题】:Load data in MVC on Button Press按下按钮时在 MVC 中加载数据
【发布时间】:2020-03-26 18:10:25
【问题描述】:

在我的主屏幕上TextBoxButton。 我需要当用户键入一个数字时,例如:111111,然后按button 这将显示与该数字关联的数据。 需要明确的是,客户必须在TextBox 中输入他们的职务(111111)并按button;按下它应该会显示数据。

这是textboxbutton

<form id="form">
        <div>
            @using (Html.BeginForm())
            {
                <p>
                    Título: @Html.TextBox("buscar")
                    <input type="submit" value="Filtrar" /><br />
                    <input type="button" value="Imprimir" onclick="window.print()" />
                </p>
            }
        </div>
        <div>
            <table border="1">
                @foreach (var item in Model)
                {
                <tr>
                    <th scope="row" abbr="Suscriptor">Suscriptor: </th>
                    <td>

                        <b>@Html.DisplayFor(modelItem => item.Apellido) @Html.DisplayFor(modelItem => item.Nombre)</b>
                    </td>
                    <td>Título: @Html.DisplayFor(modelItem => item.Titulo)</td>
                    <td>
                        @Html.ActionLink("Ver detalles en PDF", "PrintPartialViewToPdf", new { id = item.Titulo })
                    </td>
                </tr>
                }
            </table>
        </div>
    </form>

这是显示数据的查询代码:CODE UPDATED

public List<Cuotas> cargarDatos(double? tit)
        {
            List<Cuotas> salida = new List<Cuotas>();
            using (SqlConnection conn = new SqlConnection("string"))
            {
                conn.Open();
                SqlCommand comand = new SqlCommand("SELECT titulo, apellido +', '+nombre,cuotas FROM datosSuscripto WHERE titulo = @tit", conn);
                var param = comand.Parameters.Add(new SqlParameter("@tit", SqlDbType.Float));
                param.Value = tit;
                using (SqlDataReader dr = comand.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        double titulo = Convert.ToDouble(dr.GetDouble(0));
                        string nombre = dr.GetString(1);
                        double cuota = Convert.ToDouble(dr.GetDouble(2));
                        Cuotas p = new Cuotas(titulo, nombre, cuota);
                        salida.Add(p);
                    }
                }
                conn.Close();
            }
            return salida;
}

这是控制器代码:

public ActionResult Index(double? consulta)
        {
            ConexionSQL cn = new ConexionSQL();
            return View(cn.cargarDatos(consulta));
        }

编译时出现以下错误:

Incorrect syntax near '='//SOLVED

更新新错误:

System.Data.SqlClient.SqlException: 'The parameterized query '(@tit float)SELECT titulo, apellido +', '+nombre,cuotas FROM dat' expects the parameter '@tit', which was not supplied.'

有什么建议吗?有人可以帮帮我吗?

【问题讨论】:

    标签: c# sql sql-server model-view-controller


    【解决方案1】:

    SQL 查询在源代码中应始终显示为单个文字字符串。您应该(几乎)永远不要将值连接到查询本身,因为很难以不受 SQL 注入攻击的方式这样做。

    所以,也许是这样的:

        public List<Cuotas> cargarDatos(double? tit)
        {
            List<Cuotas> salida = new List<Cuotas>();
            using (SqlConnection conn = new SqlConnection("string"))
            {
                conn.Open();
                SqlCommand comand = new SqlCommand("SELECT titulo, apellido,nombre, cuotas FROM datosSuscripto WHERE titulo = @tit", conn);
                var param = comand.Parameters.Add(new SqlParameter("@tit", SqlDbType.Float));
                param.Value = tit;
                using (SqlDataReader dr = comand.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        double titulo = dr.GetDouble(0);
                        //string apellido = dr.GetString(1);
                        string nombre = dr.GetString(2);
                        double cuota = dr.GetDouble(3);
                        Cuotas p = new Cuotas(titulo, nombre, cuota);
                        salida.Add(p);
                    }
                }
                conn.Close();
            }
            return salida;
        }
    

    【讨论】:

    • 您好,感谢您的回答。现在,当我编译它显示我这个错误:System.Data.SqlClient.SqlException: 'The parameterized query '(@tit float)SELECT titulo, apellido +', '+nombre,cuotas FROM dat' expects the parameter '@tit', which was not supplied.'
    • 为您的方法设置一个断点,然后在调试时逐行检查并查看您的操作方法是否被命中,如果没有,请检查您的视图代码。看来您没有将 tit 作为参数传递。
    • 是的,但想法是参数由用户定义。所以索引应该从文本框和按钮开始。然后用户输入数字并按下按钮显示数据。但我无法编译。由于在尝试编译销售时提到的错误@AbhishekDuppati
    • 你是否指定了命令的类型,比如 cmd.CommandType = CommandType.StoredProcedure;
    【解决方案2】:

    就我而言,我遇到了这种类型的错误,原因如下:

    如果即使所有参数值都正确,也没有指定命令类型:

    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    

    如果某些参数的值为 null,则 SqlCommand 对象将忽略其参数集合中值为 null 的任何 SqlParameter 对象。 给存储过程中的每个参数一个默认值可以解决这个问题:

    @Parameter int to @Parameter int = NULL 
    

    在某些情况下,分配 DBNull.Value 而不是 Null,其中值应该是 SQL NULL 可以解决问题。

    如果您忘记为存储过程的输出参数提供默认值,您将面临此错误。

    @example int = 0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      相关资源
      最近更新 更多