Luc*_*ucy 3 asp.net asp.net-mvc
如果对象(国家/地区)不为null,我想返回一个视图.但是我收到错误"Not Not code paths返回一个值"
我的代码看起来像这样
public ActionResult Show(int id)
{
if (id != null)
{
var CountryId = new SqlParameter("@CountryId", id);
Country country = MyRepository.Get<Country>("Select * from country where CountryId=@CountryId", CountryId);
if (country != null)
{
return View(country);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当您从"if"语句中返回某些内容时会发生这种情况.编译器认为,如果"if"条件为假,该怎么办?这样,即使您在函数中定义了返回类型"ActionResult",也不会返回任何内容.所以在else语句中添加一些默认返回:
public ActionResult Show(int id)
{
if (id != null)
{
var CountryId = new SqlParameter("@CountryId", id);
Country country = MyRepository.Get<Country>("Select * from country where CountryId=@CountryId", CountryId);
if (country != null)
{
return View(country);
}
else
{
return View(something);
}
}
else
{
return View(something);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
432 次 |
| 最近记录: |