Myprofile查看布局

时间:2019-05-06 10:53:00

标签: asp.net-mvc

我需要在我的布局页面中插入“ myprofile”的base64中照片的化身。 我有一个连接到数据库的会话模型。

这是我从DB呈现img的配置文件布局:

<div class="row form-group">
                @Html.LabelFor(model => model.BinaryPhoto, htmlAttributes: new { @class = "control-label col-md-3" })
                <div class="col-md-4">
                    @if (Model.BinaryPhoto != null)
                    {
                        var base64 = Convert.ToBase64String(Model.BinaryPhoto);
                        var imgsrc = string.Format("data:image/jpg;base64,{0}", base64);
                        <img id="preview_photo" src="@imgsrc" alt="your image" style="height: 100px; width: 100px;" class="img-circle" />
                    }
                    else
                    {
                        <img id="preview_photo" src="#" alt="your image" style="display: none; height: 100px; width: 100px;" class="img-circle" />
                    }
                    <input type="file" name="Photo" id="Photo" style="width: 100%;" onchange="loadFile(event)" />
                    @*@Html.EditorFor(model => model.BinaryPhoto, null, new { htmlAttributes = new { @type = "file" } })*@
                    @Html.ValidationMessageFor(model => model.BinaryPhoto, "", new { @class = "text-danger" })
                </div>
            </div>

这是我的控制器动作:

[HttpGet]
        public ActionResult Myprofile()
        {
            var userID = ((SessionModel)Session["SessionModel"]).UserID; // get current user id
            TBL_User item = _context.TBL_User.Find(userID);
            UserModel model = new UserModel();
            model.UserID = userID;
            model.Username = item.Username;
            model.IsActive = true;
            model.Name = item.Name;
            model.BinaryPhoto = item.BinaryPhoto;
            return View(model);
        }

        [HttpPost]
        public ActionResult Myprofile(UserModel model, HttpPostedFileBase Photo)
        {
            try
            {
                var userID = ((SessionModel)Session["SessionModel"]).UserID; // get current user id
                TBL_User item = _context.TBL_User.Find(model.UserID);
                _context.Entry(item).State = EntityState.Modified; //UPDATE
                if (ModelState.IsValid)
                {
                    if (Photo != null && Photo.ContentLength > 0)
                    {
                        item.BinaryPhoto = new byte[Photo.ContentLength]; // file1 to store image in binary formate  
                        Photo.InputStream.Read(item.BinaryPhoto, 0, Photo.ContentLength);
                    }
                    using (var trans = _context.Database.BeginTransaction())
                    {
                        item.Name = model.Name;
                        //item.Email = model.Email;
                        //item.Username = model.Username;
                        //item.IsActive = model.IsActive;
                        //item.SigavUserLogin = model.SigavUserLogin;
                        //item.OperationPlaceID = model.OperationPlaceID;
                        item.LastUpdate = DateTime.UtcNow;
                        item.LastUpdateBy = userID;
                        _context.SaveChanges();
                        trans.Commit();
                    }

                    return RedirectToAction("Index");
                }
            }

,在我的布局主页面中,我在右侧有一个小“头像”,我想将myprofile的照片放在该“头像”中。

<li class="dropdown dropdown-user dropdown-dark">
                            <a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-close-others="true" aria-expanded="false">
                                <i class="fa fa-search text-symbol-color-do"></i>
                                <img alt="" class="">
                                <span class="username username-hide-on-mobile text-symbol-color-do"> @sessionModel.Username </span>
                                <i class="fa fa-angle-down text-symbol-color-do"></i>
                            </a>

我该怎么做?谢谢

1 个答案:

答案 0 :(得分:0)

如果要在布局中显示照片(这意味着它必须在所有页面上可见,而不仅仅是MyProfile页面上)。

在这种情况下,您应该在母版页上执行部分操作,以呈现问题中列出的内容。

例如,动作可以如下:

import timestring
start_period = "2019-04-22 00:00"
print(timestring.Date(start_period))

然后在cshtml中将以下行放在要显示图像的位置:

[ChildActionOnly]
public ActionResult DisplayProfilePicture() {
    var userID = ((SessionModel)Session["SessionModel"]).UserID; // get current user id
            TBL_User item = _context.TBL_User.Find(userID);
            UserModel model = new UserModel();
            model.UserID = userID;
            model.Username = item.Username;
            model.IsActive = true;
            model.Name = item.Name;
            model.BinaryPhoto = item.BinaryPhoto;
            return View(model);
}

@Html.Action("DisplayProfilePicture") 应该是局部视图,并且应该具有渲染DisplayProfilePicture标签的逻辑。

实现的详细信息可以在这里找到:https://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx/