授权:用户在登录时可以编辑自己的详细信息

时间:2018-02-17 14:19:15

标签: c# asp.net-mvc linq

我有一个应用程序,我希望志愿者能够帮助举行仪式。

我希望他们登录并点击一个按钮,这会将他们带到一个页面,显示他们的详细信息以及他们可以申请的仪式列表。我有那个页面工作,但我没办法让当前用户登录。

另外,为了澄清:我有一个志愿者实体,它持有数据,然后是一个单独的用户实体。志愿者的用户名与其用户实体相同。

我想:

  1. 将用户名与志愿者用户名进行比较并获取 VolunteerId。

    2.然后,这将用于编辑/加入该特定志愿者的仪式。

  2. 这是我的志愿者控制器方法:

    // GET: 
        public ActionResult VolunteerCeremony(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
    
    
    
            string userName = string.Empty;
    
            var getVolunteerId = (from u in db.Volunteers
                                  where WebSecurity.CurrentUserName == u.Username
                                  select u.VolunteerId).SingleOrDefault();
    
    
            Volunteer v = (Volunteer)(from k in db.Volunteers
                           where getVolunteerId == k.VolunteerId
                           select k).SingleOrDefault();
    
    
            if (v == null)
            {
                return HttpNotFound();
            }
            PopulateAssignedCeremonyData(v);
            return View(v);
        }
    
        // GET: 
        public ActionResult VolunteerHub()
        {
            return View();
    
        }
    
    
        // POST: /Player/VolunteerCeremony/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult VolunteerCeremony(int? id, string[] selectedOptions)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
    
            var getVolunteerId = (from u in db.Volunteers
                                  where WebSecurity.CurrentUserName == u.Username
                                  select u.VolunteerId).SingleOrDefault();
    
    
            var v = (Volunteer)(from k in db.Volunteers
                                      where getVolunteerId == k.VolunteerId
                                      select k).SingleOrDefault();
    
            try
            {
                UpdateVolunteerCeremonies(selectedOptions, v);
    
                db.Entry(v).State = EntityState.Modified;
                db.SaveChanges();
    
                return RedirectToAction("Index");
            }
            catch (RetryLimitExceededException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
            }
    
    
            PopulateAssignedCeremonyData(v);
            return View(v);
        }
    

    然后是我的Razor页面,第一个我希望用户点击链接将它们带到编辑/加入仪式页面的页面:

    @model PIMS.Entities.Volunteer
    
    @{
        ViewBag.Title = "VolunteerHub";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <li>@Html.ActionLink("Join Ceremony", "VolunteerCeremony", "Volunteers", null, new { id = @model.VolunteerId })</li>
    

    这给了我new { id = @model.VolunteerId }

    的错误

    然后是我想要访问的页面:

        @model PIMS.Entities.Volunteer
    @using Microsoft.AspNet.Identity
    
    @{
        ViewBag.Title = "VolunteerCeremony";
    }
    
    <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    
    <h2>Apply for Ceremony</h2>
    
    
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
            <hr />
            @Html.ValidationSummary(true)
            @Html.HiddenFor(model => model.VolunteerId)
    
            <div class="form-group">
                @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Name, new { @readonly = "readonly" })
                    @Html.ValidationMessageFor(model => model.Name)
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.VolunteerRole, "Volunteer Role", new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.VolunteerRole, new { @readonly = "readonly" })
                    @Html.ValidationMessageFor(model => model.VolunteerRole)
    
                </div>
            </div>
            </div>
    
    
    
            <div class="row">
    
                <div class="col-md-2">&nbsp;</div>
                <div class="form-group col-md-4">
                    <label class="control-label">Assigned Ceremonies</label>
                    @Html.ListBox("selectedOptions", (MultiSelectList)ViewBag.SelectedCeremonies, new { @class = "form-control" })
                </div>
    
                <div class="form-group col-md-1" style="text-align:center">
                    <div class="form-group">
                        <button type="button" id="btnRight" class="btn btn-warning btn-lg">
                            <span class="glyphicon glyphicon-arrow-right"></span>
                        </button>
                    </div>
                    <div class="form-group">
                        <button type="button" id="btnLeft" class="btn btn-success btn-lg">
                            <span class="glyphicon glyphicon-arrow-left"></span>
                        </button>
                        <div></div>
                    </div>
                </div>
    
                <div class="form-group col-md-4">
                    <label class="control-label">Available Ceremonies</label>
                    @Html.ListBox("availOptions", (MultiSelectList)ViewBag.AvailCeremonies, new { @class = "form-control" })
    
                </div>
    
                <input type="submit" id="btnSubmit" value="Save" class="btn btn-default" />
    
    
            </div>
            <div style="text-align:center;">
    
            </div>
    
    }
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
        @Scripts.Render("~/bundles/multisel")
    }
    

    有没有人对此有任何经验,因为它对我的申请来说很关键!

1 个答案:

答案 0 :(得分:0)

在mvc中有一些叫做会话变量的东西,就像app.config变量一样,你在控制器中声明它们就像这样

<header>
  <div class="logoWrap">
    <div class="logo">
      <img src="https://lorempixel.com/50/50/" alt="random logo">
    </div>
  </div>
</header>

一旦宣布,他们将在整个会议期间继续宣布。

然后你编写了注销代码,将Session变量重置为你的代码处理的东西,因为没有人登录或只是null。