我有一个两部分表单,它要求用户提供一些输入,然后在发布到两个不同的表然后将它们转发到另一个表单之前将默认值添加到其他字段。我使用的是ASP.net 4.5.1 MVC5和Code First Entity Framework。我看到的错误是"并非所有代码路径都返回一个值。"这是我的第一个EF .net项目,我可以使用帮助。谢谢。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CampaignType,Location,CampaignTitle,ShortIntro,Amount,StartDate,EndDate")] Campaign campaign)
{
if (ModelState.IsValid)
{
bool Featured = false;
string userId = User.Identity.GetUserId();
bool AcceptTerms = true;
bool Active = true;
DateTime DateAdded = DateTime.Now;
int Status = 2;
db.Campaigns.Add(campaign);
db.SaveChanges();
}
else
return View();
}
public ActionResult Create([Bind(Include = "CampaignID,Cost,Description,DeliveryDate,DeliveryDesc,MaxQuantity")] Service service)
{
if (ModelState.IsValid)
{
int Status = 2;
db.Services.Add(service);
db.SaveChanges();
return RedirectToAction("CampaignCreatorProfile", "Create");
}
return View();
}
答案 0 :(得分:2)
您的Create
方法没有if。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CampaignType,Location,CampaignTitle,ShortIntro,Amount,StartDate,EndDate")] Campaign campaign) {
if (ModelState.IsValid) {
bool Featured = false;
string userId = User.Identity.GetUserId();
bool AcceptTerms = true;
bool Active = true;
DateTime DateAdded = DateTime.Now;
int Status = 2;
db.Campaigns.Add(campaign);
db.SaveChanges();
}
else {
// Do something to handle when the Model is not valid.
}
// Move your return statement outside the scope of the else.
return View();
}