【问题标题】:How to replace replace Html.Raw text with ASP.NET Identity user data如何用 ASP.NET 身份用户数据替换替换 Html.Raw 文本
【发布时间】:2020-02-17 01:12:48
【问题描述】:

我正在尝试替换帖子正文中的内容,我正在使用所见即所得的编辑器。我正在输入文本“占位符”并尝试用我的控制器中的 ASP.NET Identity 用户数据替换它。

我有一个 ASP.NET Identity 的扩展方法:

public static string GetUserFirstName(this IIdentity identity)
{
    var db = ApplicationDbContext.Create();
    var user = db.Users.FirstOrDefault(u => u.UserName.Equals(identity.Name));

    return user != null ? user.FirstName : String.Empty;
}

这是我在控制器中的创建操作

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Image,TypeId,ModalName,Title,Description,Body,CreatedAt")] Document document, HttpPostedFileBase Image)
{
    if (ModelState.IsValid)
    {
        // set timestamp
        document.CreatedAt = DateTime.Now;

        if (!string.IsNullOrEmpty(document.Body))
        {
            document.Body.ToLower().Replace("Placeholder", User.Identity.Name);
        }

        //extract first three characters of the document title as the Id for the modal pop up
        document.ModalName = document.Title.Substring(0, 3);

        db.Documents.Add(document);
        db.SaveChanges();

        // redirect to index page
        return RedirectToAction("Index");
    }

    ViewBag.TypeId = new SelectList(db.DocumentTypes, "Id", "Type", document.TypeId);
    return View(document);
}

这是风景

<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="@item.Title" aria-hidden="true" id="@item.ModalName">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            @Html.Raw(item.Body)
        </div>
    </div>
</div>

【问题讨论】:

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


    【解决方案1】:

    我通过在控制器的 Index 操作结果中将文本替换为标识扩展方法的值来实现这一点。

    document.Body = document.Body.ToString().Replace("BusinessName", User.Identity.GetBusinessName().ToString());
    document.Body = document.Body.ToString().Replace("UserEmail", User.Identity.GetBusinessEmail().ToString());
    document.Body = document.Body.ToString().Replace("UserAddress", User.Identity.GetUserAddress().ToString());
    document.Body = document.Body.ToString().Replace("UserPhone", User.Identity.GetContactNumber().ToString());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-15
      • 2021-07-05
      • 1970-01-01
      • 2015-01-11
      相关资源
      最近更新 更多