将多行添加到数据库表razor CSHTML

时间:2016-06-16 04:43:03

标签: c# html sql asp.net razor

我试图在VS2012中编写代码,我可以输入用户名,在表格中勾选项目复选框,然后提交。然后,它会将每个项目的用户名插入数据库表中。我尝试过这样的事情,但它不会插入。

我试图在名为SectionAccess的表中添加新行。所以如果一个ID" group1"分别检查3个项目(链接1,2,3),它将在表格中插入3行(' group1',' 1'),(' group1&# 39;,' 2'),(' group1',' 3')。

@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Admin Console";

    var db = Database.Open("Testreporting");
    var sql_c = "select Link, PPMID, EPRID, Proj_Name from AdminConsole;";
    var addgroup = "insert into SectionAccess (Access, UserID, Link) values ('user', '" + Request["UserID"] + "','" + Request["add"] + "');";
    var temp = "";
    var Link = "";
    var r_group = Request["UserID"];
    int counter = 0;

    if (!Request.Params["add"].IsEmpty())
    {
        foreach(var row in db.Query(sql_c)){
            if (Request["add"+Link]=="True")
            {
                db.Execute(addgroup);
            }
        }
    }
}

<form name ="NewGroup" method="get" >
    <fieldset>
        <legend>Add New Group</legend>

        <h6>Step 1:</h6>
        <label for ="NewGroupID">Enter New Group ID: </label>
        <input type="text" name= "UserID" value="@Request.Form["UserID"]" />

        <h6>Step 2:</h6>
        <p>Add Desired Project: </p>

        <table>      
        <tr>
        <th >#</th>
        <th >PPMID</th>
        <th >EPRID</th>
        <th >Project Name</th>
        <th >Add</th>
        </tr>

        @foreach (var row in db.Query(sql_c)) {
        counter++;

        <tr class="@temp">

            <td><br />
                    @row.Link
            </td>

            <td><br />
                    @row.PPMID
            </td>

            <td ><br />
                    @row.EPRID
            </td>

            <td ><br />
                    @row.Proj_Name
            </td>

            <td><br /><input type="checkbox" id="checkbox" name="add@(row.Link)" value="@row.Link"/></td>

        </tr>            
        }
        </table>

        <br /><input style="float:right;font-weight:bold" type="submit" onclick="return confirm('Are you sure you want to add?')" id="add" name="add@(Link)" value="@Link"/>Add Project<br />

        <div class = "home">
            <br /><a href="@Href("~/AdminConsoleHome.cshtml")">Back to Homepage</a>
        </div>

    </fieldset>


</form>

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

MVC应该使用它。 索引是用户操作,主页是您的控制器。

<a href="@Url.Action("Index", "Home")">Back to Homepage</a>