简体   繁体   中英

Add multiple rows to database table razor CSHTML

I'm trying to write a code in VS2012 where I can type in a username, tick project checkboxes in a table, and submit. It will then insert that username with each project in a database table. I tried doing something like this but it won't insert.

I'm trying to add new rows into a table called SectionAccess. So if an ID "group1" checks 3 projects (with link 1,2,3) respectively, it will insert 3 rows into the table, ('group1','1'), ('group1','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>

What I'm doing wrong here?

MVC Should use this. Index is user Action and home is your controller.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM