Sql server 如何在asp.net mvc中使用TemData[]处理错误?
我想使用TempData处理以下错误: 1) 如果某些条件未满足,则我定义的自定义错误 2) 以显示确切的SQL server错误Sql server 如何在asp.net mvc中使用TemData[]处理错误?,sql-server,asp.net-mvc,Sql Server,Asp.net Mvc,我想使用TempData处理以下错误: 1) 如果某些条件未满足,则我定义的自定义错误 2) 以显示确切的SQL server错误 注意:我在代码之后使用重定向。可能是您需要这样:在控制器中 public ActionResult SaveProduct(Product model) { var ErrorString = null; // your custom validations for example, if(model.Name == nu
注意:我在代码之后使用重定向。可能是您需要这样:在控制器中
public ActionResult SaveProduct(Product model)
{
var ErrorString = null;
// your custom validations for example,
if(model.Name == null) ErrorString = "Name is empty";
try
{
// your db save operations
}
catch (Exception exception)
{
ErrorString = exception.Message;
}
if(ErrorString != null) TempData["Error"] = ErrorString;
return Redirect("YourAction");
}
鉴于:
@{
var error = (string)TempData["Error"];
}
@if (!string.IsNullOrEmpty(error))
{
<p>@Html.Raw(error)</p>
}
@{
var error=(字符串)TempData[“error”];
}
@如果(!string.IsNullOrEmpty(错误))
{
@Html.Raw(错误)
}
这是一本关于此主题的好书[在此处输入链接说明][1][1]:如果出现错误,不应重定向。