gpt4 book ai didi

c# - 获取错误 : Not all code paths return a value

转载 作者:太空狗 更新时间:2023-10-30 00:08:16 28 4
gpt4 key购买 nike

我是 mvc C# 的新手并且被卡住了。请告知如何解决此问题。我在添加时遇到错误。当我将鼠标悬停在红色波浪线上时,它显示“并非所有代码路径都返回一个值”

    public ActionResult Add(ShapeInputModel dto, FormCollection collection)
{

var model = new GeoRegions();

if (TryUpdateModel(model))
{


var destinationFolder = Server.MapPath("/App_Data/KML");
var postedFile = dto.Shape;

if (postedFile != null)
{
var fileName = Path.GetFileName(postedFile.FileName);
var path = Path.Combine(destinationFolder, fileName);
postedFile.SaveAs(path);

//Save to Database
Db.AddGeoRegions(model);
return RedirectToAction("Index");

}

return View();

}
}

最佳答案

使用这个:

public ActionResult Add(ShapeInputModel dto, FormCollection collection)
{
var model = new GeoRegions();

if (TryUpdateModel(model))
{
var destinationFolder = Server.MapPath("/App_Data/KML");
var postedFile = dto.Shape;

if (postedFile != null)
{
var fileName = Path.GetFileName(postedFile.FileName);
var path = Path.Combine(destinationFolder, fileName);
postedFile.SaveAs(path);

//Save to Database
Db.AddGeoRegions(model);
return RedirectToAction("Index");
}
return View();

}
return null; // you can change the null to anything else also.
}

发生错误是因为如果 TryUpdateModel(model) = false,您的函数不会返回任何内容。所以添加行 return nullreturn 'any other thing' 将解决问题!

关于c# - 获取错误 : Not all code paths return a value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10731097/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com