我怎么能从一个表单绑定到2个不同的表没有得到这个错误

本文关键字:错误 2个 怎么能 一个 绑定 表单 | 更新日期: 2023-09-27 18:08:09

我有一个由两部分组成的表单,它向用户请求一些输入,然后向其他字段添加默认值,然后将它们发送到两个不同的表,然后将它们转发到另一个表单。我使用ASP.net 4.5.1 MVC5和代码优先实体框架。我看到的错误是"并非所有代码路径都返回值"。这是我的第一个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();
         }    

我怎么能从一个表单绑定到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();
}