从MVC表单向数据库发布数据

3
我正在尝试将MVC表单中的联系人数据保存到SQL数据库。我还将数据保存到XML文档中。XML文档已正确创建并包含正确的数据,但是我无法将数据保存到数据库中。非常感谢您的任何帮助。
处理联系人数据的控制器:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Mvc;
using TestProject.Models;
using System.Xml.Serialization;
using System.Configuration;
using System.Windows.Forms;
using Rabbit.Bootstrap;

namespace TestProject.Controllers
{
    public class ContactsController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            Contact contact = new Contact();

            return View(contact);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(Contact c)
        {
            if (ModelState.IsValid)
            {
                string ContactFileName = Path.GetFileName(String.Format("{0} {1}.xml", c.LastName, c.FirstName));
                ContactFileName = (@"C:\Users\kevin.schultz\TestDocuments\" + ContactFileName);
                if (System.IO.File.Exists(ContactFileName))
                {
                    MessageBox.Show("The file already exists. A number will be added to create a unique file name", "Important", MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                    int i = 0;
                    while (System.IO.File.Exists(ContactFileName))
                    {
                        ContactFileName = String.Format("{0} - {1}", ContactFileName, i.ToString());
                        i++;
                    }
                }

                XmlSerializer ser = new XmlSerializer(c.GetType());
                StreamWriter myWriter = new StreamWriter(ContactFileName);
                ser.Serialize(myWriter, c);
                myWriter.Close();
                return View("ContactSuccess");

            }
            return View("ContactError");
        }

        [HttpGet]
        public ActionResult Save()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Save (Contact s)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    using (ContactDataEntities CustData = new ContactDataEntities())
                    {
                        CustData.dbContacts.Add(
                            new dbContact()
                            {
                                FirstName = s.FirstName,
                                LastName = s.LastName,
                                PhoneHome = s.PhoneHome,
                                PhoneCell = s.PhoneCell,
                                Email = s.Email,
                                Address = s.Address,
                                City = s.City,
                                State = s.State,
                                ZipCode = s.ZipCode,
                            });
                        CustData.SaveChanges();

                        return View("ContactSuccess");
                    }
                }
                catch (Exception ex)
                {
                    return View("ContactError");
                }
            }

            else
            {
                MessageBox.Show("You are missing data, please ensure all fields are correct.", "Important", MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
            return View();
        }
    }
}

数据库

CREATE TABLE [dbo].[Table] (
    [Id]        INT  NOT NULL,
    [FirstName] TEXT NOT NULL,
    [LastName]  TEXT NOT NULL,
    [PhoneHome] TEXT NOT NULL,
    [PhoneCell] TEXT NOT NULL,
    [Email]     TEXT NOT NULL,
    [Address]   TEXT NOT NULL,
    [City]      TEXT NOT NULL,
    [State]     TEXT NOT NULL,
    [ZipCode]   TEXT NOT NULL,

模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace TestProject.Models
{

        public class Contact 
        {
            [Required]
            [DisplayFormat(ConvertEmptyStringToNull = false)]
            [StringLength(20, MinimumLength = 2, ErrorMessage = "Enter A Valid First Name")]
            public string FirstName { get; set; }

            [Required(ErrorMessage = "Enter A Valid Last Name")]
            [DisplayFormat(ConvertEmptyStringToNull = false)]
            [StringLength(20, MinimumLength = 2)]
            public string LastName { get; set; }

            [Required(ErrorMessage = "Enter A Valid Home Phone Number including area code")]
            [DataType(DataType.PhoneNumber)]
            public string PhoneHome { get; set; }

            [Required(ErrorMessage = "Enter A Valid Cell Phone Number")]
            [DataType(DataType.PhoneNumber)]
            public string PhoneCell { get; set; }

            [Required(ErrorMessage = "Enter A Valid Email Address")]
            [DataType(DataType.EmailAddress)]
            public string Email { get; set; }

            [Required(ErrorMessage = "Enter A Valid Street Address")]
            [DisplayFormat(ConvertEmptyStringToNull = false)]
            [StringLength(120, MinimumLength = 4)]
            public string Address { get; set; }

            [Required(ErrorMessage = "Enter A Valid City Name")]
            [DisplayFormat(ConvertEmptyStringToNull = false)]
            [StringLength(30, MinimumLength = 2)]
            public string City { get; set; }

            [Required(ErrorMessage = "Enter A Valid State Abbreviation")]
            [DisplayFormat(ConvertEmptyStringToNull = false)]
            [StringLength(20, MinimumLength = 2)]
            public string State { get; set; }

            [Required(ErrorMessage = "Enter A Valid 5 Digit Zip Code")]
            [DataType(DataType.PostalCode)]
            public string ZipCode { get; set; }
        }


    }

查看

@model TestProject.Models.Contact

@{
    ViewBag.Title = "Contact Page";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h4>Company Contact Form</h4>
<br />
@using (Html.BeginForm())
{ 
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <form class="form-primary">
        <fieldset class="form-group">
            <div class="form-label">
                @Html.LabelFor(model => model.FirstName, "First Name", new { style = "display:inline;" })
            </div>
            <div class="form.field">
                @Html.EditorFor(model => model.FirstName, new { placeholder = "First Name" })
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>

        <div class="form-label">
            @Html.LabelFor(model => model.LastName, "Last Name", new { style = "display:inline;" })
        </div>

        <div class="form.field">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>

        <div class="form-label">
            @Html.LabelFor(model => model.PhoneHome, "Home Phone", new { style = "display:inline;" })
        </div>

        <div class="form.field">
            @Html.EditorFor(model => model.PhoneHome)
            @Html.ValidationMessageFor(model => model.PhoneHome)
        </div>

        <div class="form-label">
            @Html.LabelFor(model => model.PhoneCell, "Cell Phone", new { style = "display:inline;" })
        </div>

        <div class="form.field">
            @Html.EditorFor(model => model.PhoneCell)
            @Html.ValidationMessageFor(model => model.PhoneCell)
        </div>

        <div class="form-label">
            @Html.LabelFor(model => model.Email, "Email Address", new { style = "display:inline;" })
        </div>

        <div class="form.field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="form-label">
            @Html.LabelFor(model => model.Address, "Street Address", new { style = "display:inline;" })
        </div>

        <div class="form.field">
            @Html.EditorFor(model => model.Address)
            @Html.ValidationMessageFor(model => model.Address)
        </div>

        <div class="form-label">
            @Html.LabelFor(model => model.City, "City", new { style = "display:inline;" })
        </div>

        <div class="form.field">
            @Html.EditorFor(model => model.City)
            @Html.ValidationMessageFor(model => model.City)
        </div>

        <div class="form-label">
            @Html.LabelFor(model => model.State, "State", new { style = "display:inline;" })
        </div>

        <div class="form.field">
            @Html.EditorFor(model => model.State)
            @Html.ValidationMessageFor(model => model.State)
        </div>

        <div class="form-label">
            @Html.LabelFor(model => model.ZipCode, "Zip Code", new { style = "display:inline;" })
        </div>

        <div class="form.field">
            @Html.EditorFor(model => model.ZipCode)
            @Html.ValidationMessageFor(model => model.ZipCode)

        </div>

    <br />
    <div>
        <input type="submit" class="btn-custom" value="Submit" />
        <input type="reset" class="btn-custom" value="Clear" />
    </div>

        </fieldset>
    </form>
}

2
你在调试器中运行过它吗?你是否遇到了错误?你是使用 SQL Server 还是本地文件?仅从代码转储中很难诊断多层问题。 - D Stanley
我认为这是因为您正在将数据发布到Index操作,而该操作将数据保存到XML而不是数据库。当您发布到Save操作时,无法保存到数据库吗?您是否进入了Save操作?因此,请提供更多详细信息。 - Sefa
我在这里也没有看到任何明显的问题。然而,有几件事情值得指出:1)您的文件写入不是线程安全的。即使您正在追加数字,仍然可能发生冲突。当然,这需要两个具有相同名称的人同时发布,这不太可能,但是可能会导致错误的双重发布之类的事情。2)对于EF工作,使用“using”是一个坏主意。要么将上下文创建为实例变量,要么将其注入控制器中。3)Html.BeginForm输出一个<form>元素,因此您的视图中有两个表单。 - Chris Pratt
在写入数据库之前,我使用了调试器并且应用程序没有错误,但在创建XML文件后出现了问题。至于发布到保存操作,我不确定如何判断是否进入了保存操作。 - Kevin Schultz
1个回答

2
您实际上从未调用保存到数据库的方法。这是我的建议:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(Contact c)
{
    if (!ModelState.IsValid)
    {
        //should use client side validation for this as well.
            MessageBox.Show("You are missing data, please ensure all fields are correct.", "Important", MessageBoxButtons.OK,
                MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
        return View();
    }
    SaveToXml(c);
    return SaveToDatabase(c);

}

public void SaveToXml(Contact c)
{
    string ContactFileName = Path.GetFileName(String.Format("{0} {1}.xml", c.LastName, c.FirstName));
    ContactFileName = (@"C:\Users\kevin.schultz\TestDocuments\\" + ContactFileName);
    if (System.IO.File.Exists(ContactFileName))
    {
        MessageBox.Show("The file already exists. A number will be added to create a unique file name", "Important", MessageBoxButtons.OK,
        MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

        int i = 0;
        while (System.IO.File.Exists(ContactFileName))
        {
            ContactFileName = String.Format("{0} - {1}", ContactFileName, i.ToString());
            i++;
        }
    }

    XmlSerializer ser = new XmlSerializer(c.GetType());
    StreamWriter myWriter = new StreamWriter(ContactFileName);
    ser.Serialize(myWriter, c);
    myWriter.Close();
}

public ActionResult SaveToDatabase(Contact s)
{
    try
    {
        using (ContactDataEntities CustData = new ContactDataEntities())
        {
            CustData.dbContacts.Add(
                new dbContact()
                {
                    FirstName = s.FirstName,
                    LastName = s.LastName,
                    PhoneHome = s.PhoneHome,
                    PhoneCell = s.PhoneCell,
                    Email = s.Email,
                    Address = s.Address,
                    City = s.City,
                    State = s.State,
                    ZipCode = s.ZipCode,
                });
            CustData.SaveChanges();

            return View("ContactSuccess");
        }
    }
    catch (Exception ex)
    {
        return View("ContactError");
    }
}

我为了更清晰的表达对代码进行了重构。从你的视图中没有调用保存方法,所以我假设你想要在一次点击中同时保存。反斜杠是转义标记,主要是因为Stackoverflow不能识别\"作为闭合引号(\也是转义字符)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接