如何将int与int model json和mvc 4进行比较

时间:2022-09-19 22:52:07

I try to compare a int variable received by parameter with int field in DB.

我尝试将参数接收的int变量与DB中的int字段进行比较。

Function in Controller:

控制器中的功能:

[AcceptVerbs(HttpVerbs.Get)]
    public JsonResult getServicoID(string serie, int numDoc)
    {
        try
        {
            var result = db.Servicos.Where(dados => dados.DadosComerciais.Serie == serie && dados.DadosComerciais.NumDoc == numDoc); // i think the problem is here - dados.DadosComerciais.NumDoc == numDoc
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            return Json(new { Result = "ERROR", Message = ex.Message }, JsonRequestBehavior.AllowGet);
        }
    }

Function js:

函数js:

function AddServContratado() {
//Buscar ServicoID da tabela servicos para guardar na ServicoContratado

$.getJSON("/Contrato/getServicoID", { serie: $("#Serie").val(), numDoc: $("#NumDoc").val() },
      function (result) {
          var servicoID = result.ServicosID;
          alert(result.ServicosID);
      });

2 个解决方案

#1


1  

I find the solution:

我找到了解决方案:

Controller:

控制器:

[AcceptVerbs(HttpVerbs.Get)]
    public JsonResult getServicoID(string serie, int numDoc)
    {
        try
        {
            var result = db.Servicos.FirstOrDefault(dados => dados.DadosComerciais.Serie == serie && dados.DadosComerciais.NumDoc == numDoc);

            return Json(result.ServicosID, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            return Json(new { Result = "ERROR", Message = ex.Message }, JsonRequestBehavior.AllowGet);
        }
    }

Js:

JS:

    $.getJSON("/Contrato/getServicoID", { serie: $("#Serie").val(), numDoc: $("#NumDoc").val() },
      function (result) {
          var servicoID = result;
          alert(result);
      });

#2


0  

You are returning a list from your controller action. The result variable is an IEnumerable<Servico>.

您将从控制器操作返回一个列表。结果变量是IEnumerable

And in your javascript file you are attempting to use some result.ServicosID property which cannot work because you have a list of objects. You could access them for example like this: result[0].ServicosID.

在你的javascript文件中,你试图使用一些result.ServicosID属性,因为你有一个对象列表,所以它无法工作。您可以访问它们,例如:result [0] .ServicosID。

If you want to access a single object then make sure you have returned a single object from your controller action:

如果要访问单个对象,请确保已从控制器操作返回单个对象:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetServicoID(string serie, int numDoc)
{
    try
    {
        var result = db.Servicos.FirstOrDefault(dados => dados.DadosComerciais.Serie == serie && dados.DadosComerciais.NumDoc == numDoc);
        if (result == null)
        {
            // no object found in the database that matches the criteria => 404
            return HttpNotFound();
        }
        return Json(result, JsonRequestBehavior.AllowGet);
    }
    catch (Exception ex)
    {
        Response.StatusCode = 500;
        return Json(new { Result = "ERROR", Message = ex.Message }, JsonRequestBehavior.AllowGet);
    }
}

#1


1  

I find the solution:

我找到了解决方案:

Controller:

控制器:

[AcceptVerbs(HttpVerbs.Get)]
    public JsonResult getServicoID(string serie, int numDoc)
    {
        try
        {
            var result = db.Servicos.FirstOrDefault(dados => dados.DadosComerciais.Serie == serie && dados.DadosComerciais.NumDoc == numDoc);

            return Json(result.ServicosID, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            return Json(new { Result = "ERROR", Message = ex.Message }, JsonRequestBehavior.AllowGet);
        }
    }

Js:

JS:

    $.getJSON("/Contrato/getServicoID", { serie: $("#Serie").val(), numDoc: $("#NumDoc").val() },
      function (result) {
          var servicoID = result;
          alert(result);
      });

#2


0  

You are returning a list from your controller action. The result variable is an IEnumerable<Servico>.

您将从控制器操作返回一个列表。结果变量是IEnumerable

And in your javascript file you are attempting to use some result.ServicosID property which cannot work because you have a list of objects. You could access them for example like this: result[0].ServicosID.

在你的javascript文件中,你试图使用一些result.ServicosID属性,因为你有一个对象列表,所以它无法工作。您可以访问它们,例如:result [0] .ServicosID。

If you want to access a single object then make sure you have returned a single object from your controller action:

如果要访问单个对象,请确保已从控制器操作返回单个对象:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetServicoID(string serie, int numDoc)
{
    try
    {
        var result = db.Servicos.FirstOrDefault(dados => dados.DadosComerciais.Serie == serie && dados.DadosComerciais.NumDoc == numDoc);
        if (result == null)
        {
            // no object found in the database that matches the criteria => 404
            return HttpNotFound();
        }
        return Json(result, JsonRequestBehavior.AllowGet);
    }
    catch (Exception ex)
    {
        Response.StatusCode = 500;
        return Json(new { Result = "ERROR", Message = ex.Message }, JsonRequestBehavior.AllowGet);
    }
}