Asp.net mvc 4 如何向数据库添加新信息?

Asp.net mvc 4 如何向数据库添加新信息?,asp.net-mvc-4,azure-storage,Asp.net Mvc 4,Azure Storage,我正在使用以下代码。我已经设置了添加项所需的表单、模型和控制器。我知道代码是有效的,因为在文本字段中输入文本后,我被重定向到正确的页面。我的问题是:如何将用户输入的文本添加到默认数据库中 public ActionResult TruckAdd() { ViewBag.DisplayText = "Add A Truck"; return View(); } [HttpPost] public ActionResult

我正在使用以下代码。我已经设置了添加项所需的表单、模型和控制器。我知道代码是有效的,因为在文本字段中输入文本后,我被重定向到正确的页面。我的问题是:如何将用户输入的文本添加到默认数据库中

    public ActionResult TruckAdd()
    {
        ViewBag.DisplayText = "Add A Truck";

        return View();
    }

    [HttpPost]
    public ActionResult TruckAdd(RegisterTruckModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the Truck
            try
            {
                return RedirectToAction("Index", "Dashboard");
            }
            catch
            {

            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

这可能对您有所帮助:。您必须首先设置数据库来存储数据(创建一个卡车表,或者首先使用实体框架代码)。是的,您可以从asp.net/mvc/tutorials了解基本的mvc