简体   繁体   English

asp.net web 应用后台隐藏数据字段

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

I am making a web application and I used entity based EF designer from database model I want to hide modified by and modified Date for the backend and it shouldn't display to the user Can someone help how do I make this work?我正在制作一个 web 应用程序,我使用了来自数据库 model 的基于实体的 EF 设计器我想隐藏后端的修改日期和修改日期,它不应该显示给用户有人可以帮助我如何完成这项工作吗? I have attached my picture and code below please let me know if there is any question.我在下面附上了我的图片和代码,如果有任何问题,请告诉我。

Picture:图片:在此处输入图像描述 Controller CODE: 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);
}

} }

} ` }`

I tried to figure out but I don't know how to hide these two modified date and modified by at the backend.我想弄清楚,但我不知道如何在后端隐藏这两个修改日期和修改者

View code查看代码

<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>

Often times the entity types stored in the database have fields you don't want to, can't, or shouldn't expose on the UI.通常,存储在数据库中的实体类型具有您不想、不能或不应在 UI 上公开的字段。

If you're not concerned about the data being present and would just like to "hide" it, simply don't include those in the table markup.如果您不关心存在的数据并且只想“隐藏”它,那么只需不要将它们包含在表标记中即可。

If however you don't want the fields present at all, a common way to get around that is to create a DTO class that only has the fields you wish to expose.但是,如果您根本不希望出现这些字段,一种常见的解决方法是创建一个仅包含您希望公开的字段的 DTO class。 Update your UI to target the DTO and remove the columns from the table.更新您的 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 { ... }

} 

If you need to update the fields you dropped after an update for example, load the entity type using the primary key and update it.例如,如果您需要更新在更新后删除的字段,请使用主键加载实体类型并更新它。 This assumes you have some way to populate the current user.这假设您有一些方法来填充当前用户。

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