簡體   English   中英

asp.net web 應用后台隱藏數據字段

[英]asp.net web application hide data field at the backend

我正在制作一個 web 應用程序,我使用了來自數據庫 model 的基於實體的 EF 設計器我想隱藏后端的修改日期和修改日期,它不應該顯示給用戶有人可以幫助我如何完成這項工作嗎? 我在下面附上了我的圖片和代碼,如果有任何問題,請告訴我。

圖片:在此處輸入圖像描述 Controller 代碼:

// GET: Contract
public ActionResult Index()
{
    return View(db.Contracts.ToList());
}

// GET: Contract/Details/5
public ActionResult Details(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Contract contract = db.Contracts.Find(id);
    if (contract == null)
    {
        return HttpNotFound();
    }
    return View(contract);
}

// GET: Contract/Create
public ActionResult Create()
{
    return View();
}

// POST: Contract/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for 
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
\[HttpPost\]
\[ValidateAntiForgeryToken\]
public ActionResult Create(\[Bind(Include = "Id,ContractName,ContractNumber,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate")\] Contract contract)
{
    if (ModelState.IsValid)
    {
        db.Contracts.Add(contract);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(contract);
}

// GET: Contract/Edit/5
public ActionResult Edit(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Contract contract = db.Contracts.Find(id);
    if (contract == null)
    {
        return HttpNotFound();
    }
    return View(contract);
}

// POST: Contract/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for 
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
\[HttpPost\]
\[ValidateAntiForgeryToken\]
public ActionResult Edit(\[Bind(Include = "Id,ContractName,ContractNumber,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate")\] Contract contract)
{
    if (ModelState.IsValid)
    {
        db.Entry(contract).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(contract);
}

// GET: Contract/Delete/5
public ActionResult Delete(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Contract contract = db.Contracts.Find(id);
    if (contract == null)
    {
        return HttpNotFound();
    }
    return View(contract);
}

// POST: Contract/Delete/5
\[HttpPost, ActionName("Delete")\]
\[ValidateAntiForgeryToken\]
public ActionResult DeleteConfirmed(int id)
{
    Contract contract = db.Contracts.Find(id);
    db.Contracts.Remove(contract);
    db.SaveChanges();
    return RedirectToAction("Index");
}

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        db.Dispose();
    }
    base.Dispose(disposing);
}

}

}`

我想弄清楚,但我不知道如何在后端隱藏這兩個修改日期和修改者

查看代碼

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Contract</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ContractName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ContractName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ContractName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ContractNumber, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ContractNumber, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ContractNumber, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.CreatedBy, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CreatedBy, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CreatedBy, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.CreatedDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CreatedDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CreatedDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ModifiedBy, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ModifiedBy, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ModifiedBy, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ModifiedDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ModifiedDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ModifiedDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <button style="background-color:white; border-color:darkgrey;"><input type="submit" value="Create" class="btn btn-default" /></button>
</div>
        </div>
    </div>
}

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

<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

通常,存儲在數據庫中的實體類型具有您不想、不能或不應在 UI 上公開的字段。

如果您不關心存在的數據並且只想“隱藏”它,那么只需不要將它們包含在表標記中即可。

但是,如果您根本不希望出現這些字段,一種常見的解決方法是創建一個僅包含您希望公開的字段的 DTO class。 更新您的 UI 以定位 DTO 並從表中刪除列。

public class ContractDto
{
    // all the fields you want to expose

    // Ctor
    public ContractorDto(Contract contract) { ... }

    // "From" pattern
    public static ContractDto From(Contract contract)
        => new ContractDto { ... }

    // An implicit operator can also be helpful
    public static implicit operator ContractDto(Contract contract)
        => new ContractDto { ... }

} 

例如,如果您需要更新在更新后刪除的字段,請使用主鍵加載實體類型並更新它。 這假設您有一些方法來填充當前用戶。

var entity = db.Contracts.Find(contractDto.Id);
// update entity from dto as needed
entity.ModifiedBy = // populate the user from context or similar 
entity.ModifiedDate = DateTime.Now;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM