在ASP.NET

时间:2018-01-19 14:43:54

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

我在ASP.NET MVC中面临一些编辑和删除SQL行的问题。我面临的主要问题是从我的控制器中的url中获取QueryString值。

由于某种原因,我无法附加Request.QueryString["Username"]。它给了我Can not apply indexing with [] to an expression of type QueryString错误。

我做错了什么或有什么方法可以通过我的控制器将QueryParameter传递给我的数据访问层,在那里它将执行所需的查询。

我对EditDelete

都有同样的问题

这是我的代码到目前为止:

ShowUsers.cshtml (此处隐藏了编辑和删除操作)

@model IEnumerable<SecureMedi.Models.Users>

<section id="users">
    <table id="users-table" class="table table-bordered table-responsive">
        <thead>
            <tr>
                <th class="sort">Username</th>
                <th class="sort">Role</th>
                <th class="sort"><i class="md-icon dp18">mode_edit</i></th>
                <th class="sort"><i class="md-icon dp18">delete</i></th>
            </tr>
        </thead>
        <tbody class="list">
            @foreach (var item in Model) {
            <tr>
                <td class="username">@item.Username</td>
                <td class="role">@item.Role</td>
                <td class="actions">@Html.ActionLink("Edit", "EditUser", "Home", new { Username = @item.Username }, null)</td>
                <td class="actions">@Html.ActionLink("Delete", "DeleteUser", "Home", new { Username = @item.Username }, null)</td>
            </tr>
            }
        </tbody>
    </table>
</section>

EditUser.cshtml (用于编辑) - 注意:此处我还希望默认显示Username中的QueryString(用户名=某个名称)页面加载时形成。当从EditUser页面转到ShowUsers操作时,url看起来像/Home/EditUser?Username=somename

@model SecureMedi.Models.Users


<section id="edit-user">
    <h4 class="sub-title space-top space-bottom">Edit user</h4>
    <form id="edit-user-form" asp-controller="Home" asp-action="EditUser" method="post">
        <div class="form-group">
            <label asp-for="Username" class="form-control-label">Username</label>
            <input asp-for="Username" type="text" class="form-control" id="username" name="Username" required disabled value="" />
        </div><!-- / form-group -->
        <div class="form-group">
            <label asp-for="Role">Role</label>
            <select asp-for="Role" class="form-control selector" id="role" name="Role" required>
                <option value="" selected disabled>Select role</option>
                <option value="SecureMediUsers">SecureMediUsers</option>
                <option value="SecureMediModerators">SecureMediModerators</option>
                <option value="SecureMediAdministrators">SecureMediAdministrators</option>
            </select>
        </div><!-- / form-group -->
        <button type="submit" class="btn btn-primary">Save</button>
    </form><!-- / form -->
</section>

HomeController.cs

[HttpGet]
public IActionResult EditUser() {
    var model = new Users();
    return View(model);
}

[HttpPost]
public IActionResult EditUser(Users u) {
    if (!ModelState.IsValid) {
        return View(u);
    }
    UsersDAL ud = new UsersDAL();
    ud.Edit(u);
    return RedirectToAction("ShowUsers");
}

[HttpPost]
public IActionResult DeleteUser(Users u) {
    if (!ModelState.IsValid) {
        return View(u);
    }
    UsersDAL ud = new UsersDAL();
    ud.Delete(u);
    return RedirectToAction("ShowUsers");
}

UsersDAL.cs (数据访问层)

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using SecureMedi.Models;

namespace SecureMedi.DAL {
    public class UsersDAL {
        public void Edit(Users u) {
            string connectionstring = "MY_CONNECTION_STRING";
            string sql = String.Format("ALTER ROLE {0} DROP MEMBER {1}", u.Role, u.Username);
            string sql2 = String.Format("ALTER ROLE {0} ADD MEMBER {1}", u.Role, u.Username);
            SqlConnection conn = new SqlConnection(connectionstring);
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlCommand cmd2 = new SqlCommand(sql2, conn);

            try {
                conn.Open();
                using(conn) {
                    cmd.Transaction = conn.BeginTransaction();
                    cmd.ExecuteNonQuery();
                    cmd2.Transaction = cmd.Transaction;
                    cmd2.ExecuteNonQuery();
                    cmd2.Transaction.Commit();
                }
            } finally {
                if (conn != null) {
                    conn.Close();
                }
            }

        }

        public void Delete(Users u) {
            string connectionstring = "MY_CONNECTION_STRING";
            string sql = String.Format("DROP USER {0}", u.Username);
            SqlConnection conn = new SqlConnection(connectionstring);
            SqlCommand cmd = new SqlCommand(sql, conn);

            try {
                conn.Open();
                using(conn) {
                    cmd.ExecuteNonQuery();
                }
            } finally {
                if (conn != null) {
                    conn.Close();
                }
            }
        }
    }
}

Users.cs (型号)

using System.ComponentModel.DataAnnotations;

namespace SecureMedi.Models {
    public class Users {
        [Required]
        [StringLength(100)]
        public string Username {
            get;
            set;
        }

        [Required]
        [StringLength(100)]
        public string Role {
            get;
            set;
        }
    }
}

总之,我想解决的问题是:

1)将Username查询参数从EditUser视图和Role值从表单传递到我的EditUser控制器函数。

2)我已经连接了删除操作,如

<td class="actions">@Html.ActionLink("Delete", "DeleteUser", "Home", new { Username = @item.Username }, null)</td>

但我对如何避免[HttpGet] DeleteUser行为感到困惑。当我点击锚标记时,它会转到/Home/DeleteUser?Username=somename,但我只想执行DeleteUser操作而不更改视图。

另外,如何将Username查询参数传递给控制器​​操作。

2 个答案:

答案 0 :(得分:1)

1)将[HttpGet] EditUser更改为UserName参数: 并通过UserName让用户将此数据发送到模型:

[HttpGet]
    public IActionResult EditUser(string UserName) {
        var model = new Users(UserName);//probably, Or something like GetUser(UserName)
        return View(model);
    }

2)您无法使用actionLink调用HTTPPost方法。它将始终使用HttpGet。因此,您可以使用两种方法 - 1.删除HttpGet中的用户,然后重定向到上一页 2.使用JavaScript调用HttpPost,而不是通过动作链接点击发送HttpGet。

答案 1 :(得分:0)

您只想删除记录表单数据库,只需单击删除链接,而不确认用户应导航到同一页面。 这些改变将为你做到这一点!试过

更新您的家庭控制器,如: 我只是从delete actionResult中删除[httppost]并更改参数名称,并在返回redirecttoaction中也写入控制器名称

IMP注意:将参数用户更新为用户名,你可以像在DAL中那样使用它同时建议在任何地方都使用相同的用户名或者它是空的

HomeController.cs

[HttpGet]
public IActionResult EditUser() {
    var model = new Users();
    return View(model);
}

[HttpPost]
public IActionResult EditUser(Users u) {
    if (!ModelState.IsValid) {
        return View(u);
    }
    UsersDAL ud = new UsersDAL();
    ud.Edit(u);
    return RedirectToAction("ShowUsers", "ControllerName");
}

public IActionResult DeleteUser(Username u) {
    if (!ModelState.IsValid) {
        return View(u);
    }
    UsersDAL ud = new UsersDAL();
    ud.Delete(u);
    return RedirectToAction("ShowUsers", "ControllerName");
}