繁体   English   中英

asp.net MVC 5控制器和角色模型表

[英]asp.net MVC 5 controller and form for role model

这是角色的控制器,问题是我如何创建表单以从表单或视图中插入名为“ Admins”的角色。 提前致谢

public ActionResult CreateRole()
{
   string Output = "";
   ApplicationDbContext db = new ApplicationDbContext();
   RoleManager<IdentityRole> RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));

   if (!RoleManager.RoleExists("Admins"))
   {
      IdentityResult Result = RoleManager.Create(new IdentityRole("Admins"));
      if (Result.Succeeded)
      {
          Output = "the role created";
      }
      else
      {
          int ErrorCount = Result.Errors.Count();
          Output = "Errors is: " + Result.Errors.ToList()[0];
      }
   }
   else
   {
      Output = "the roles exist";
   }
   return Content(Output);
}

右键单击ActionName(在您的caes中为CreateRole命名的方法名称),从ContextMenu中单击AddView这将使对话框出现,指明您的需要并接受默认值。 在视图中添加以下代码

@using(Html.BeginForm()) {
    <input type="submit" value="Create Role" />
}

您还必须在ActionName上方添加以下属性,如下所示

[HttpPost]
public ActionResult CreateRole()

现在,当您从视图中单击“创建角色”按钮时,您的代码必须开始执行,您可以在方法开始时设置一个断点来对其进行调试

注意:还请注意,您必须添加get操作,如果尚未添加视图,它将显示视图本身,您必须编写以下代码

[ActionName("CreateRole")]
public ActionResult GetCreateRole()
{
    return View();
}

另外,您需要指定该视图不采用任何模型,以使此代码起作用。

如果您是ASP.NET MVC的新手,并且不了解它的基本知识,则可以查阅此重要文章 ,了解如何使用MVC

首先,您需要创建一个数据模型,如下所示:

public class NewRole
{
    public string Name { get; set; }
}

之后,您需要编辑控制器以发送NewRole模型,并使用具有相同名称的post方法获取它。

    public ActionResult CreateRole()
    {

        NewRole newRole = new NewRole();
        return View(newRole);
    }
    [HttpPost]
    public ActionResult CreateRole(NewRole newRole)
    {
        string Output = "";
        ApplicationDbContext db = new ApplicationDbContext();
        RoleManager<IdentityRole> RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));

        if (!RoleManager.RoleExists("Admins"))
        {
            IdentityResult Result = RoleManager.Create(new IdentityRole(newRole.Name));
            if (Result.Succeeded)
            {
                Output = "the role created";
            }
            else
            {
                int ErrorCount = Result.Errors.Count();
                Output = "Errors is: " + Result.Errors.ToList()[0];
            }
        }
        else
        {
            Output = "the roles exist";
        }

        if (Output == "")
        {
            ModelState.AddModelError(string.Empty, "tesssst");
            return View(newRole);
        }
        else
        {
            return Redirect("Suscces url");
        }

    }

最后,右键单击CreateRole方法,然后按创建视图。

创建这样的视图:

在此处输入图片说明

之后,您将获得一个简单的视图,该视图显示您的错误消息。 记住在您的视图中写一些CSS

@model WebApplication1.Models.NewRole

@{
    ViewBag.Title = "CreateRole";
}

<h2>CreateRole</h2>
@using (Html.BeginForm())
{
    @Html.LabelFor(x => x.Name, new { @class = "LoginText" })
    @Html.TextBoxFor(x => x.Name, new { @class = "form-control" })
    @Html.ValidationMessageFor(x => x.Name)
    @Html.ValidationSummary()
    <button type="submit">Save</button>
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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