onclick按钮保存数据.cshtml

时间:2016-04-07 09:13:34

标签: c# asp.net-mvc button razor onclick

我有一个asp.net mvc razor文件。该按钮需要将数据发送到数据库。这是我的代码。数据库已连接并在服务器上运行。我只是无法获取按钮将数据发送到数据库。使用javascript的onclick事件我已经尝试但似乎没有任何工作。有人可以解释我应该做什么吗?这是我第一次使用.cshtml,我真的不知道如何解决这个问题。

问题得到解决。 我使用了错误的名称..我在.cshtml的C#部分中引用了输入的id而不是名称..

@{
Validation.RequireField("achternaam", "Achternaam is verplicht.");
Validation.RequireField("voornaam", "Voornaam is verplicht.");
Validation.RequireField("organisatie", "Organisatie is verplicht.");
Validation.RequireField("email", "Email is verplicht.");
Validation.RequireField("datum", "Datum is verplicht.");
Validation.RequireField("tijd", "Tijd is verplicht.");

var achternaam = "";
var voornaam = "";
var organisatie = "";
var email = "";
var datum = "";
var tijd = "";

if (IsPost && Validation.IsValid())
{
    achternaam = Request.Form["achternaam"];
    voornaam = Request.Form["voornaam"];
    organisatie = Request.Form["organisatie"];
    email = Request.Form["email"];
    datum = Request.Form["datum"];
    tijd = Request.Form["tijd"];


    using (var db = new Blub._1.Database.blubEntities())
    {
        // Insert
        var bezoeker = new Blub._1.Database.visitor
        {
            AchterNaam = achternaam,
            VoorNaam = voornaam,
            Bedrijf = organisatie,
            DatumBezoek = Convert.ToDateTime(datum),
            Email = email
        };                   

    Response.Redirect("~Bezoeker");
}

<div id="header">
    <h1>Als voorbeeld ...</h1>
</div>

<div id="nav">
    <br />
    <a href="/OverzichtWerknemer">Overzicht</a>
    <br />
    <a href="/Bezoeker">Nieuwe bezoeker</a>
    <br />
    <a href="/Contractor">Nieuwe contractor</a>
</div>

<div id="section">
    <form name="login" action="~/OverzichtWerknemer.cshtml" method="post" accept-charset="utf-8">
        <table>
            <tr>
                <td><b>Achternaam*:</b></td>
                <td>
                    <input id="achternaam" name="LastName" type="text" maxlength="60" style="width:146px; border:1px solid #999999" required />
                </td>
            </tr>
            <tr>
                <td><b>Voornaam*:</b></td>
                <td>
                    <input id="voornaam" name="FirstName" type="text" maxlength="60" style="width:146px; border:1px solid #999999" required/>
                </td>
            </tr>
            <tr>
                <td><b>Organisatie*:</b></td>
                <td><input id="organisatie" name="Organization" type="text" maxlength="60" style="width:300px; border:1px solid #999999" required /></td>
            </tr>
            <tr>
                <td><b>Emailadres*:</b></td>
                <td><input id="email" name="FromEmailAddress" type="text" maxlength="60" style="width:300px; border:1px solid #999999" required/></td>
            </tr>
            <tr>
                <td><b>Dag van bezoek*:</b></td>
                <td><input id="datum" name="FromDate" type="date" style="width:300px; border:1px solid #999999" required /></td>
            </tr>
            <tr>
                <td><b>Tijdstip*</b></td>
                <td><input id="tijd" name="TimeVisit" type="time" maxlength="60" style="width:300px; border:1px solid #999999" required /></td>
            </tr>
            <tr>
                <td colspan="2">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    * - Verplicht veld. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="submit" value="Registreer" />

                </td>
            </tr>
        </table>
        <br />
    </form>
</div>

<div id="footer">
    Copyright
</div>

2 个答案:

答案 0 :(得分:1)

最好的方法是使用网址助手

<form method="post" action="@Url.Action("MyAction", "MyController")" >

答案 1 :(得分:1)

我认为你必须有一个带有这样一个动作的表格标签:

<form method="post" action="/ControllerName/ActionName">

或者使用#Kypaz建议的助手:

@using (Html.BeginForm("ActionName", "ControllerName", null (adapt to route), FormMethod.Post))
{

 <input type="submit" value="Submit"/>  

}