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

时间:2016-10-30 10:49:13

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

这是Roles的控制器,问题是我如何创建表单以插入名为" 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);
}

2 个答案:

答案 0 :(得分:1)

右键单击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的greate article

答案 1 :(得分:0)

首先,您需要创建一个这样的数据模型:

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方法并按下创建视图。

创建如下视图:

enter image description here

之后,您将看到一个简单的视图,显示您的错误消息。记得在你的视图中写一些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>
}
相关问题