溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

五、Asp.Net MVC4.0開發(fā)CMS系統(tǒng)案例之用戶信息修改模塊

發(fā)布時(shí)間:2020-07-16 16:45:57 來源:網(wǎng)絡(luò) 閱讀:1811 作者:sujerhy 欄目:編程語(yǔ)言

    用戶信息Modely已經(jīng)有了,所以不需要編寫這塊,直接實(shí)現(xiàn)Contraller即可。

    首頁(yè)要修改用戶個(gè)人信息必須是在登錄狀態(tài)下才能有權(quán)限,因此我們當(dāng)客戶登錄成功之后,默認(rèn)進(jìn)入個(gè)人的管理中心。所以需要有個(gè)默認(rèn)的Action. 

        /// <summary>
        /// 默認(rèn)的用戶個(gè)人信息中心
        /// </summary>
        /// <returns></returns>
       [UserAuthorize]
        public ActionResult Default()
        {
            var _userInfo = userRpy.Find(LoginName);
            ViewData["UserType"] = Common.Function.getUserTypeList(_userInfo.UserType);
            ViewData["Flag"] = Common.Function.getUserFlagList(_userInfo.Flag);  
            return View(_userInfo);
        }

    讀取當(dāng)期登錄人的所有用戶信息,返回到View頁(yè)面顯示。點(diǎn)擊右鍵添加視圖

    五、Asp.Net MVC4.0開發(fā)CMS系統(tǒng)案例之用戶信息修改模塊

    調(diào)整View頁(yè)面代碼布局與內(nèi)容:

@model Hillstone.Models.SysComUser

@{
    ViewBag.Title = "個(gè)人信息";
}
<div class="user-nav float-left">@Html.Partial("PartialUserNav")</div>
<fieldset class="float-left">
    <legend>個(gè)人信息</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.LoginName)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.LoginName)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.UserName)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.UserName)
    </div>


    <div class="display-label">
         @Html.DisplayNameFor(model => model.OrderNo)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.OrderNo)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.UserType)
    </div>
    <div class="display-field">
        @ViewData["UserType"].ToString()
    </div>

    <div class="display-field">
        @Html.DisplayNameFor(model => model.Flag)
    </div>

    <div class="display-label">
         @ViewData["Flag"].ToString()
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.UnitId)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.UnitId)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.PosId)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.PosId)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.CreatedUserName)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CreatedUserName)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.CreatedDate)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CreatedDate)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.UserId }) |
    @Html.ActionLink("Back to List", "Index")
</p>

    二、用戶信息修改

        #region 修改信息
        /// <summary>
        /// 修改用戶信息默認(rèn)頁(yè)面
        /// </summary>
        /// <returns>URL</returns>
        [UserAuthorize]
        public ActionResult UserChangeInfo()
        {
            var _userInfo = userRpy.Find(LoginName);
            ViewData["Flag"] = Common.Function.getUserFlagList(_userInfo.Flag);
            return View(_userInfo);
        }

        /// <summary>
        /// 提交修改用戶數(shù)據(jù)
        /// </summary>
        /// <param name="userInfo">用戶數(shù)據(jù)實(shí)體</param>
        /// <returns>URL</returns>
        [UserAuthorize]
        [HttpPost]
        public ActionResult UserChangeInfo(SysComUser userInfo)
        {
            ViewData["Flag"] = Common.Function.getUserFlagList(userInfo.Flag);
            //對(duì)要做修改的用戶,檢查輸入的密碼是否正確
            if (userRpy.Authentication(LoginName, userInfo.Password) == 0)
            {
                //讀取數(shù)據(jù)庫(kù)中該用戶是否還存在
                var _userInfo = userRpy.Find(userInfo.LoginName);
                _userInfo.UserName = userInfo.UserName;
                _userInfo.UserType = userInfo.UserType;
                _userInfo.UnitId = userInfo.UnitId;
                _userInfo.PosId = userInfo.PosId;
                _userInfo.Flag = userInfo.Flag;
                if (userRpy.Update(_userInfo))
                {
                    ModelState.AddModelError("Message", "修改成功!");
                    return View();

                }
                else
                {
                    ModelState.AddModelError("Message", "修改失敗!");
                    return View();
                }
            }
            else 
            {
                ModelState.AddModelError("Password", "輸入的密碼錯(cuò)誤!");
                return View();
            }
        }
        #endregion

    修改用戶信息時(shí)候首先確認(rèn)登錄身份[UserAuthorize],其次輸入密碼的是否正確Authentication

再?gòu)臄?shù)據(jù)庫(kù)讀取當(dāng)前登錄名的數(shù)據(jù)Find,最后執(zhí)行更新Update.

@model Hillstone.Models.SysComUser

@{
    ViewBag.Title = "UserChangeInfo";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h3>UserChangeInfo</h3>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <div class="user-nav float-left">@Html.Partial("PartialUserNav")</div>
    <fieldset class="float_left">
        <legend>SysComUser</legend>

        @Html.HiddenFor(model => model.UserId)

        <div class="editor-label">
            @Html.LabelFor(model => model.LoginName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LoginName)
            @Html.ValidationMessageFor(model => model.LoginName)
            @Html.DisplayDescriptionFor(model=>model.LoginName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
            @Html.DisplayDescriptionFor(mode => mode.UserType)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.OrderNo)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.OrderNo)
            @Html.ValidationMessageFor(model => model.OrderNo)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.UserType)
        </div>
        <div class="editor-field">
            @Html.RadioButtonFor(model=>model.UserType,0) 企業(yè)內(nèi)部
            @Html.RadioButtonFor(model=>model.UserType,1) 企業(yè)外部
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Flag)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model=>model.Flag,(List<SelectListItem>)ViewData["Flag"])
            @Html.ValidationMessageFor(model => model.Flag)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.UnitId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UnitId)
            @Html.ValidationMessageFor(model => model.UnitId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.PosId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PosId)
            @Html.ValidationMessageFor(model => model.PosId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CreatedUserId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CreatedUserId)
            @Html.ValidationMessageFor(model => model.CreatedUserId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CreatedUserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CreatedUserName)
            @Html.ValidationMessageFor(model => model.CreatedUserName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CreatedDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CreatedDate)
            @Html.ValidationMessageFor(model => model.CreatedDate)
        </div>

        <p>
            <input type="submit" value="Save" />@Html.ValidationMessage("Message")
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

radioButton和DropDownList的控件在編輯頁(yè)面使用方法:


        <div class="editor-field">
            @Html.RadioButtonFor(model=>model.UserType,0) 企業(yè)內(nèi)部
            @Html.RadioButtonFor(model=>model.UserType,1) 企業(yè)外部
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model=>model.Flag,(List<SelectListItem>)ViewData["Flag"])
            @Html.ValidationMessageFor(model => model.Flag)
        </div>


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI