【问题标题】:How do i return custom error message in the generic list C#如何在通用列表 C# 中返回自定义错误消息
【发布时间】:2016-08-23 08:03:55
【问题描述】:

我是 C# 语言的新手,请帮助。 如果模型为空,我希望返回此消息我收到错误消息

代码:

public List<PatQuery> GetPatientslist(PatQuery PatQmodel)
    {
        if(PatQmodel != null)
        {
            var mem_no = PatQmodel.memberno;
            var results = _database.Query<PatQuery>("Select * from pat_data where memberno=@0 ", mem_no);
            List<PatQuery> list = results.ToList();
            return list;
        }
      else
        {
            AddMessage("E0000", "null request.", CMessageType.Error);
            return;
        }

    }

消息是这样的。

private void AddMessage(string _Code, string _Message, CMessageType _MessageType)
    {
        validateRes.Add(new CMessage()
        {
            Code = _Code,
            Message = _Message,
            MessageType = _MessageType

        });
    } 

确实,我还可以对 validateRes 之类的消息执行其他操作,但是否可以返回此消息

【问题讨论】:

  • 如果您谈论将消息返回到视图,我建议使用 ViewBag

标签: c# asp.net-mvc asp.net-web-api generic-list


【解决方案1】:

将错误消息传递到 ViewBag 中,然后在 View 中显示消息。

控制器

public ActionResult Index()
{
    modelType model = new modelType()
    //GetModel here
    If(model == null)
    {
        ViewBag.ErrorMsg = "Emtpy Model";
    }

    return View();
}

查看

@if(ViewBag.ErrorMsg != null)
{
    <label>@ViewBag.ErrorMsg</label>
}

你也可以像这样抛出HttpException

throw new HttpException(400, "No data was found");

【讨论】:

    猜你喜欢
    • 2020-06-16
    • 2019-10-18
    • 1970-01-01
    • 2021-11-27
    • 2019-01-03
    • 2015-08-13
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    相关资源
    最近更新 更多