寻找一种更好的方法来删除按钮ASP.NET MVC的角色 [英] Looking for a better way to remove role with button ASP.NET MVC

查看:74
本文介绍了寻找一种更好的方法来删除按钮ASP.NET MVC的角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我现在有这个工作我有一个提交按钮,目前用于分配角色。我有第二个按钮来删除角色。要删除角色,我调用$ .ajax调用,然后将带有id的控制器传递给我的操作。这工作但我的TempData [msg]没有被存储,因此当调用结束时,没有显示消息,就像我添加角色一样。在我的动作方法结束时,我的'return redirectToAction()'也没有刷新角色。

这就是我有的方法我可以让那些小东西工作或者什么是还有更多的想法吗?

谢谢你,

  //   VIEW这是我从这个DropDownList获取我的userId的地方@Html.AntiForgeryToken() 

< div class = row >
< div class = col-md-4 >
@ Html.DropDownListFor(a = > a.UserId,
new SelectList(Model.lstAdmins, UserId 名称),
new {@class = form-control,@ id = userAdmins })
@ *,@ onChange = SelectedValue(this) * @
@ Html.HiddenFor(a = > a.UserId)
@ Html.ValidationMessageFor(a = > a.UserId)
< / div >
< div class = col-md -4 > < / div >
< div class = col-md-4 > < / div >
< / div > < div class = row >
@for( var i = 0 ; i < Model.lstUsers.Count(); i ++)
{
< div class = col-md-4 >
< div>
@ Html.CheckBoxFor(u = > Model.lstUsers [i] .SelectedUsers)
< label>
@ Html.DisplayFor(u = > Model.lstUsers [i] .Name)
@ Html.HiddenFor(u = > Model.lstUsers [i] .UserId)
@ Html.HiddenFor(u = > 模型.lstUsers [i] .Name)
< / label >
< / div >
< / div >
}
< / div >
< br /&g吨;
< div class = row >
< div class = form-group >
< div < span class =code-keyword> class = col-md-offset-0 col-md -12 >
< input type = 提交 value = 分配角色 class = btn btn-success />
@ * @ Url.Action( RemoveAdmin SuperAdmin)* @

< button type = 按钮 onclick = clickMe( )
class = btn btn-danger >
删除角色
< / 按钮 >

@ Html.ActionLink( 取消 仪表板 SuperAdmin
null new {@class = btn btn-danger})
< / div > < script 类型 = text / javascript < span class =code-keyword>>
// $(function() {

函数clickMe(){
var userid = $( #userAdmins)VAL();
if (userid!= ){
$ .ajax({
url: RemoveAdmin / SuperAdmin
数据:{userid:userid},
类型: POST
// success:function(){
// alert(成功);
/ / },
// 错误:function(){
// alert(错误);
// }
});
}
// window.location.href =
< span class =code-comment> //
'Url.Action(RemoveAdmin,SuperAdmin)/'+ userid;
// alert(val+ selectedValue);
}
< span class =code-comment> // CONTROLLER ACTION
[HttpPost]
// [ValidateAntiForgeryToken]
public ActionResult RemoveAdmin( string userid)
{
ApplicationUser au = context.Users.Where(u = > u.Id.Equals(userid,
StringComparison.CurrentCultureIgnoreCase))。FirstOrDefault();
var store = new UserStore< ApplicationUser>(context);
var manager = new UserManager< ApplicationUser>(商店);
manager.RemoveFromRole(au.Id, Admin);
manager.AddToRole(au.Id, User);

TempData [ 成功] = 成功分配的角色;
return RedirectToAction( AssignAdmin);
}





我的尝试:



我使用我的控制器为一个角色创建了一个行动,一个没有任何问题的角色,我有两个方法可以让所有当前用户都有某些角色。我希望这可以调用我的用途,以便它可以在下拉列表中显示正确的用户以太,如果他们是管理员,或者如果他们是普通用户则在lstUsers中。我使用javascript获取值和aajax来进行调用,但它不能100%工作我想要

解决方案

.ajax调用我通过我的控制器id我的行动这工作但我的TempData [msg]没有被存储,因此当调用结束时,没有显示消息,就像我添加角色一样。在我的动作方法结束时,我的'return redirectToAction()'也没有刷新角色。

这就是我有的方法我可以让那些小东西工作或者什么是还有更多的想法吗?

谢谢你,

  //   VIEW这是我从这个DropDownList获取我的userId的地方@Html.AntiForgeryToken() 

< div class = row >
< div class = col-md-4 >
@ Html.DropDownListFor(a = > a.UserId,
new SelectList(Model.lstAdmins, UserId 名称),
new {@class = form-control,@ id = userAdmins })
@ *,@ onChange = SelectedValue(this) * @
@ Html.HiddenFor(a = > a.UserId)
@ Html.ValidationMessageFor(a = > a.UserId)
< / div >
< div class = col-md -4 > < / div >
< div class = col-md-4 > < / div >
< / div > < div class = row >
@for( var i = 0 ; i < Model.lstUsers.Count(); i ++)
{
< div class = col-md-4 >
< div>
@ Html.CheckBoxFor(u = > Model.lstUsers [i] .SelectedUsers)
< label>
@ Html.DisplayFor(u = > Model.lstUsers [i] .Name)
@ Html.HiddenFor(u = > Model.lstUsers [i] .UserId)
@ Html.HiddenFor(u = > 模型.lstUsers [i] .Name)
< / label >
< / div >
< / div >
}
< / div >
< br /&g吨;
< div class = row >
< div class = form-group >
< div < span class =code-keyword> class = col-md-offset-0 col-md -12 >
< input type = 提交 value = 分配角色 class = btn btn-success />
@ * @ Url.Action( RemoveAdmin SuperAdmin)* @

< button type = 按钮 onclick = clickMe( )
class = btn btn-danger >
删除角色
< / 按钮 >

@ Html.ActionLink( 取消 仪表板 SuperAdmin
null new {@class = btn btn-danger})
< / div > < script 类型 = text / javascript < span class =code-keyword>>
//


(function(){

函数clickMe(){
var userid =

#userAdmins)。val();
if (userid!= ){


Hello all,
I currently have this working I have a submit button currently being used to assign roles. I have a second button to remove the roles. To remove the roles I call an $.ajax call and I pass my controller with the id to my action. This works BUT my TempData[msg] isn’t being stored so when the call finishes no message is displayed like when I add a role. Also the Roles are not being refreshed with my ‘return redirectToAction()’ at the end of my action method.
Here is what I have is there a way I can get those small things working or what are some more ideas to do this?
Thank you,

//VIEW This is where I am getting my userId from this DropDownList @Html.AntiForgeryToken()

            <div class="row">
                <div class="col-md-4">
                    @Html.DropDownListFor(a => a.UserId,
                     new SelectList(Model.lstAdmins, "UserId", "Name"),
                     new { @class = "form-control", @id = "userAdmins" })
                    @* , @onChange = "SelectedValue(this)"*@
                    @Html.HiddenFor(a => a.UserId)
                    @Html.ValidationMessageFor(a => a.UserId)
                </div>
                <div class="col-md-4"></div>
                <div class="col-md-4"></div>
            </div><div class="row">
                @for(var i = 0; i < Model.lstUsers.Count(); i++)
                {
                    <div class="col-md-4">
                        <div>
                            @Html.CheckBoxFor(u => Model.lstUsers[i].SelectedUsers)
                            <label>
                                @Html.DisplayFor(u => Model.lstUsers[i].Name)
                                @Html.HiddenFor(u => Model.lstUsers[i].UserId)
                                @Html.HiddenFor(u => Model.lstUsers[i].Name)
                            </label>
                        </div>
                    </div>
                }
            </div>
            <br />
            <div class="row">
                <div class="form-group">
                    <div class="col-md-offset-0 col-md-12">
                        <input type="submit" value="Assign Role" class="btn btn-success" />
                        @* @Url.Action("RemoveAdmin", "SuperAdmin") *@

                        <button type="button" onclick="clickMe()"
                                class="btn btn-danger">
                            Remove Role
                        </button>

                        @Html.ActionLink("Cancel", "Dashboard", "SuperAdmin",
                         null, new { @class = "btn btn-danger" })
                    </div>                                                          <script type="text/javascript">
    //$(function () {

    function clickMe() {
        var userid = $("#userAdmins").val();
        if (userid != "") {
            $.ajax({
                url: "RemoveAdmin/SuperAdmin",
                data: { userid: userid },
                type: "POST",
                //success: function () {
                //    alert("Success");
                //},
                //error: function () {
                //    alert("Error");
                //}
            });
        }
        //window.location.href =
            //'Url.Action("RemoveAdmin", "SuperAdmin")/' + userid;
        //alert("val" + selectedValue);
    }
// CONTROLLER ACTION
[HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult RemoveAdmin(string userid)
        {
            ApplicationUser au = context.Users.Where(u => u.Id.Equals(userid,
                StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
            var store = new UserStore<ApplicationUser>(context);
            var manager = new UserManager<ApplicationUser>(store);
            manager.RemoveFromRole(au.Id, "Admin");
            manager.AddToRole(au.Id, "User");

            TempData["Success"] = "Roles Assigned Successfully";
            return RedirectToAction("AssignAdmin");
        }



What I have tried:

I used my controller to create a action to addusers to a role that works without any issses and I have 2 methods that gets all the current users with there certain roles. I want this to call my uses so it can display the correct users ether in the dropdown if they are admin or in the lstUsers if they are a normal user. I used javascript to get the value and aajax to make the call but its not working 100% how I want

解决方案

.ajax call and I pass my controller with the id to my action. This works BUT my TempData[msg] isn’t being stored so when the call finishes no message is displayed like when I add a role. Also the Roles are not being refreshed with my ‘return redirectToAction()’ at the end of my action method.
Here is what I have is there a way I can get those small things working or what are some more ideas to do this?
Thank you,

//VIEW This is where I am getting my userId from this DropDownList @Html.AntiForgeryToken()

            <div class="row">
                <div class="col-md-4">
                    @Html.DropDownListFor(a => a.UserId,
                     new SelectList(Model.lstAdmins, "UserId", "Name"),
                     new { @class = "form-control", @id = "userAdmins" })
                    @* , @onChange = "SelectedValue(this)"*@
                    @Html.HiddenFor(a => a.UserId)
                    @Html.ValidationMessageFor(a => a.UserId)
                </div>
                <div class="col-md-4"></div>
                <div class="col-md-4"></div>
            </div><div class="row">
                @for(var i = 0; i < Model.lstUsers.Count(); i++)
                {
                    <div class="col-md-4">
                        <div>
                            @Html.CheckBoxFor(u => Model.lstUsers[i].SelectedUsers)
                            <label>
                                @Html.DisplayFor(u => Model.lstUsers[i].Name)
                                @Html.HiddenFor(u => Model.lstUsers[i].UserId)
                                @Html.HiddenFor(u => Model.lstUsers[i].Name)
                            </label>
                        </div>
                    </div>
                }
            </div>
            <br />
            <div class="row">
                <div class="form-group">
                    <div class="col-md-offset-0 col-md-12">
                        <input type="submit" value="Assign Role" class="btn btn-success" />
                        @* @Url.Action("RemoveAdmin", "SuperAdmin") *@

                        <button type="button" onclick="clickMe()"
                                class="btn btn-danger">
                            Remove Role
                        </button>

                        @Html.ActionLink("Cancel", "Dashboard", "SuperAdmin",
                         null, new { @class = "btn btn-danger" })
                    </div>                                                          <script type="text/javascript">
    //


(function () { function clickMe() { var userid =


("#userAdmins").val(); if (userid != "") {


这篇关于寻找一种更好的方法来删除按钮ASP.NET MVC的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆