gpt4 book ai didi

asp.net-mvc - ASP.NET MVC : Prevent user from editing certain fields on the model

转载 作者:行者123 更新时间:2023-12-04 00:46:40 24 4
gpt4 key购买 nike

我有一个模型,它有几个我不想让用户编辑的字段。我如何创建一个编辑页面,它不仅可以隐藏这些字段,还可以防止用户自己注入(inject)输入元素?

谢谢

最佳答案

看看 TryUpdateModel()(MSDN article found here .)。您可以指定您希望该方法实际更新的内容的黑名单和白名单:

public ActionResult MyUpdateMethod (MyModel myModel)
{
if (ModelState.IsValid)
{
var myDomainModel = new DomainModel ();
if (TryUpdateModel (myDomainModel,
new string[] { /* WhiteList Properties here */ },
new string[] { /* BlackList Properties here */ })

{
// Save it or do whatever
return RedirectToActionV (/* Yada */);
}
}

return View (myModel);
}
}

除此之外,我会确保您发布的模型不包含您希望用户更新的字段,尽管可以使用手工制作的表单发布来覆盖这些字段。这至少会帮助您确保您不会不小心将字段放在您不想编辑的页面上。

关于asp.net-mvc - ASP.NET MVC : Prevent user from editing certain fields on the model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8752909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com