如何 return 对象模型值

how to return the object model value

我是 c# 的新手,我正在尝试 return IHttpActionResult 方法中的一个值,但是它显示错误名称 objApprovalModel 在当前上下文中不存在。

这是我的代码:

[HttpPost]
        [Route("GetApprovalTableValue")]
        public IHttpActionResult GetApprovalTableValue(string rowno)
        { 
            try
            {
                

                List<ApprovalModel> approvalModels = new List<ApprovalModel>();

                //var objUserModel = new UserModel();


                ManageUserData ObjManageUserData = new ManageUserData();
                var sqlDataTable = ObjManageUserData.GetApprovalTableValue(rowno);

                if (sqlDataTable.Rows.Count > 0)
                {

                    for (int i = 0; (i < sqlDataTable.Rows.Count); i++)
                    {
                        var objApprovalModel = new ApprovalModel();

                        objApprovalModel.MasterID = Convert.ToInt32(sqlDataTable.Rows[i]["TAT_Master_ID"].ToString());
                        objApprovalModel.EntityType = Convert.ToInt32(sqlDataTable.Rows[i]["TAT_EntityTypeId"].ToString());
                        objApprovalModel.ApproverID = (sqlDataTable.Rows[i]["TAT_Approver_ID"].ToString());
                        objApprovalModel.Remarks = (sqlDataTable.Rows[i]["TAT_Remarks"].ToString());
                        objApprovalModel.Action = Convert.ToInt32(sqlDataTable.Rows[i]["TAT_Action"].ToString());
                        objApprovalModel.Active = (sqlDataTable.Rows[i]["TAT_IS_ACTIVE"].ToString());
                        objApprovalModel.CreatedBy = (sqlDataTable.Rows[i]["TAT_CREATED_BY"].ToString());
                        objApprovalModel.CreatedOn = (DateTime)(sqlDataTable.Rows[i]["TAT_CREATED_ON"]);
                        objApprovalModel.UpdatedBy = (sqlDataTable.Rows[i]["TAT_UPDATED_BY"].ToString());
                        objApprovalModel.UpdatedOn = (DateTime)(sqlDataTable.Rows[i]["TAT_UPDATED_ON"]);
                       

                    }
                }
                return Ok(objApprovalModel);


            }

            catch (Exception ex)
            {
                return Content(HttpStatusCode.NoContent, "Something went wrong");
            }

        }

审批模型是这样的:

public class ApprovalModel
    {
        public int MasterID { get; set; }
        public int EntityType { get; set; }
        public string ApproverID { get; set; }
        public string Remarks { get; set; }
        public int Action { get; set; }
        public string Active { get; set; }

        public string CreatedBy { get; set; }

        public DateTime CreatedOn { get; set; }

        public string UpdatedBy { get; set; }

        public DateTime UpdatedOn { get; set; }
    }

如何删除此错误并为我的 api 设置 return 值?

您在较小范围内声明了变量,并试图在该范围外引用它。

考虑一下...如果 if 语句是 false 或者如果 for 循环有 0 迭代次数?

要么 return 来自其声明的范围:

if (sqlDataTable.Rows.Count > 0)
{
    for (int i = 0; (i < sqlDataTable.Rows.Count); i++)
    {
        var objApprovalModel = new ApprovalModel();
        //...

        return Ok(objApprovalModel);
    }
}
// return something else here

或者首先在该范围之外声明它:

var objApprovalModel = new ApprovalModel();

if (sqlDataTable.Rows.Count > 0)
{
    for (int i = 0; (i < sqlDataTable.Rows.Count); i++)
    {
        //...
    }
}
return Ok(objApprovalModel);