简体   繁体   English

我没有从 ASP.NET MVC 中的控制器获取值

[英]I am not getting values from controller in ASP.NET MVC

I am writing an application in ASP.NET MVC 5, with Entity Framework 6.0, that based on a selected date, I send this date to a controller that passes it to a stored procedure, this procedure based on that date, counts the number of people and returns the value to the controller and "supposedly" should pass it back to ajax, so that using JavaScript can process it and show the result in the view;我正在使用 Entity Framework 6.0 在 ASP.NET MVC 5 中编写一个应用程序,该应用程序基于选定的日期,将此日期发送到控制器,该控制器将其传递给存储过程,此过程基于该日期,计算people 并将值返回给控制器,并且“应该”应该将其传递回 ajax,以便使用 JavaScript 可以处理它并在视图中显示结果; the code is the following:代码如下:

$("#FechConsult").change('click', function () {
        $.ajax({
            type: 'POST',
            dataType: 'Json',
            url: "/Home/CuentaUser",
            data: { FechaInsc: $("#FechConsult").val() },
            success: function (data) {
                var CantidadUsr = result[0];
                $("#TotCupos").val(CantidadUsr.CantiPer);
                cupos = ParseInt(CantidadUsr.CantiPer - TotalUsr);
                if ((cupos >= 15) || (cupos <= 20)) {
                    $("#TotCupos").css({ "background": "#b5ff33" });
                }
                else {
                    if ((cupos >= 10) || (cupos <= 14)) {
                        $("#TotCupos").css({ "background": "#e9f00e" });
                    }
                    else {
                        if ((cupos >= 1) || (cupos <= 9)) {
                            $("#TotCupos").css({ "background": "#e85e0a" });
                            $("#mensaje").show();
                            $("#mensaje").val("Ha Llegado Casi Al Limite de Cupos Diarios");
                        }
                        else {
                            if ((cupos = TotalUsr)) {
                                $("#mensaje").show();
                                $("#mensaje").val("Ha Llegado A Límite Máximo de Personas, Seleccione Otro Dia");
                                $("#PassWord").focus();
                            }
                        }
                    }
                }
            }
        })
    });

then this happens to the controller:然后这发生在控制器上:

[HttpPost]
    public ActionResult CuentaUser(Inscripcion inscripcion)
        {
            using (CtaUsr = new Drillco_InscripcionEntities())
            {
                var CantiPer =  CtaUsr.SP_Sel_Cta_PersXFecha(Convert.ToString(inscripcion.FechaInsc));
                return Json(CantiPer, JsonRequestBehavior.AllowGet);
            }
        }

The stored procedure is as follows:存储过程如下:

ALTER procedure [dbo].[SP_Sel_Cta_PersXFecha]
    @FechInsc varchar(10)
AS
BEGIN
    SELECT COUNT(idOper) AS CantP
    FROM Inscripcion
    WHERE FechaInsc = CONVERT(datetime, @FechInsc, 103)
END

The problem: is that after calling the stored procedure, the ajax does not receive the value returned by the controller, when I do F12 in the browser, it tells me that there is a 500 error and it does not recognize the name of the controller, I have given a thousand turns and I have not reached any solution, could someone tell me that I am doing wrong?问题是:调用存储过程后,ajax没有接收到控制器返回的值,在浏览器中按F12,提示出现500错误,无法识别控制器名称,我已经转了一千圈了,我还没有找到任何解决方案,有人能告诉我我做错了吗?

One thing, your ajax call is passing FechaInsc: $("#FechConsult").val() but your stored procedure is expecting a parameter called 'inscripcion`.一件事,您的 ajax 调用正在传递FechaInsc: $("#FechConsult").val()但您的存储过程需要一个名为 'inscripcion` 的参数。 These names & types should match.这些名称和类型应该匹配。 From what it looks like your Ajax call is sending a string for the updated "inscription" for an object, but the server-side code is expecting an entity back.从看起来您的 Ajax 调用正在为对象的更新的“铭文”发送一个字符串,但服务器端代码期望返回一个实体。

It should probably be something more like:它可能应该更像:

public ActionResult CuentaUser(string fechaInsc)
{
    using (CtaUsr = new Drillco_InscripcionEntities())
    {
        var CantiPer =  CtaUsr.SP_Sel_Cta_PersXFecha(fechaInsc);
        return Json(CantiPer, JsonRequestBehavior.AllowGet);
    }
}

However, from this if that stored procedure is updating a value on a specific record, I don't see how it will know which row to update.但是,如果该存储过程正在更新特定记录上的值,我不知道它将如何知道要更新哪一行。 Normally for an Update method you'd pass enough data to identify the row to update, and the values:通常对于 Update 方法,您会传递足够的数据来标识要更新的行,以及值:

public ActionResult CuentaUser(int inscriptionId, string fechaInsc)

Or a data container (a POCO ViewModel or DTO, Not an entity) that contains the ID(s) and data to be updated.或者包含要更新的 ID 和数据的数据容器(POCO ViewModel 或 DTO,不是实体)。

It is worth noting that the whole point of using Entity Framework is to replace concerns about talking to a data layer.值得注意的是,使用实体框架的全部意义在于取代对与数据层对话的担忧。 Define the entities, configure EF to map those relationships to the respective tables, and let EF manage the generation and execution of the SQL statements.定义实体,配置 EF 以将这些关系映射到相应的表,并让 EF 管理 SQL 语句的生成和执行。 If the intention is to write stored procedures to handle updates and selects, then you're better off just using ADO.Net.如果打算编写存储过程来处理更新和选择,那么最好只使用 ADO.Net。

With EF, an update method would look more like:使用 EF,更新方法看起来更像是:

public ActionResult CuentaUser(int inscripcionId, string fechaInsc)
{
    using (context = new DrillcoDbContext())
    {
        var inscripcion = context.Inscripcions.Single(x => x.InscripcionId == inscripcionId);
        inscripcion.FechaInsc = fechaInsc;
        context.SaveChanges();
        return Json(inscripcion, JsonRequestBehavior.AllowGet);
    }
}

This is making a lot of guesses about the objects your application would be working with.这是对您的应用程序将使用的对象进行大量猜测。 However the fundamental difference would be to pass the ID of the entity to update along with the updated value(s), use the DbContext to fetch that entity by ID, update the value, and call SaveChanges on the DbContext .然而,根本的区别在于传递要更新的实体的 ID 以及更新的值,使用DbContext通过 ID 获取该实体,更新值,并在DbContext上调用SaveChanges EF builds the SQL needed for the values that have changed if and only if those values actually change.且仅当这些值实际发生更改,EF 才会为已更改的值构建所需的 SQL。 No writing/calling stored procedures required.无需编写/调用存储过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM