C#System.Data.SqlClient插入并保存

时间:2016-12-07 09:32:40

标签: c# asp.net asp.net-mvc

我想为用户创建一个带有文本框(或其他内容)的页面,用户可以在其中填写文本框,按添加。这需要保护数据库的信息安全。 我试图用System.Data.SqlClient做这件事;但它不会起作用。 你能帮帮我吗?

控制器:

./public

索引页:

@model IEnumerable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DNDB.Models;
using System.Data.SqlClient;


namespace DNDB.Controllers
{
public class DomeinnaamController : Controller
{
    // GET: Domeinnaam
    public ActionResult Index()
    {
        var entities = new DomeinnaambeheerEntities1();

        return View(entities.Domeinnaam.ToList());
    }



    public ActionResult CreateDomainName()
    {
        return View();
    }

    [HttpPost]
    public ActionResult CreateDomainName(Domeinnaam model)
    {
        if (ModelState.IsValid)
        {
            try
            {
                SqlDataAdapter.Update(DomeinnaambeheerEntities1.Tables["Domeinnaam"]);
            }
            catch (Exception e) 
            {
                MessageBox.Show("Er is een fout opgetreden bij het update  van de tabel.");
            }

        }

        return RedirectToAction("Index");
    }

}
}

部分视图

@Styles.Render("~/Content/StyleSheet.css")
@{
ViewBag.Title = "Index";
}
<h2>Domeinnaam Overzicht</h2>

@Html.ActionLink("Voeg toe", "CreateDomainName") 


<table id="tabledomeinnamen">
<tr>
    <th>    
        @Html.DisplayNameFor(model => model.IsActief) 
    </th>
    <th> 
        @Html.DisplayNameFor(model => model.Naam) 
    </th>
    <th>
        @Html.DisplayNameFor(model => model.TLD)
    </th>
    <th>    
        @Html.DisplayNameFor(model => model.DatumRegistratie)
    </th> 
    <th>
        @Html.DisplayNameFor(model => model.Omschrijving)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.DatumOpzeg)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.EigenaarID)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Opmerking)
    </th>
    <th> 
        @Html.DisplayNameFor(model => model.BeheerAccountID)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.KlantID)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.RegistrarID)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.BetaaldVan)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.BetaaldTot)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.AfspraakPrijs)
    </th>
    <th></th>
</tr>

@Html.Partial("PartDomeinnaam", Model)


</table>

在这个页面上,我需要代码将信息添加到数据库中:

@model IEnumerable<DNDB.Models.Domeinnaam>

@foreach (var item in Model)
{
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.IsActief)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Naam)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.TLD)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.DatumRegistratie)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Omschrijving)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.DatumOpzeg)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.EigenaarID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Opmerking)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.BeheerAccountID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.KlantID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.RegistrarID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.BetaaldVan)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.BetaaldTot)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.AfspraakPrijs)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id = item.ID })
        @Html.ActionLink("Details", "Details", new { id = item.ID })
        @Html.ActionLink("Delete", "Delete", new { id = item.ID })
    </td>
</tr>
}

1 个答案:

答案 0 :(得分:1)

使用Html Helpers绑定模型

<label>IsActief</label>
  @Html.CheckBoxFor(m=>m.isActief)
<br /> 
<label>Naam</label><br />
  @Html.TextBoxFor(m=>m.name)

其他人也一样,从顶部删除IENumerable