我目前刚接触并使用Visual Studio Community / ASP.net,遵循使用Visual Studio Express的教程。似乎我遇到了障碍,因为我的表单似乎没有像在教程中那样提交给LocalDB。
显然,因为VS Express(我没有使用)安装预先配置了LocalDB实例。我通过单击工具>连接到我的SQL Server LocalDb;连接数据库。
我的问题是:为什么我的表单中输入的数据没有被提交并存储在我的SQL Server数据库中的动态创建表中,就像在教程中一样?任何帮助都感激不尽。
模型
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace ExploreCalifornia.Models
{
public class MyDbContext : DbContext
{
public MyDbContext() : base("Explore California")
{
}
public DbSet<Tour> Tours { get; set; }
}
}
控制器:
public class TourController : Controller
{
private MyDbContext db = new MyDbContext();
// GET: Tour
public ActionResult Index()
{
return View();
}
[HttpPost] //<--explicitly identified (difference from the other create() action above
public ActionResult Create(Tour tour)
{
try
{
// TODO: Add insert logic here
db.Tours.Add(tour);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}