我有两个具有一对多关系的对象(LabTest & LabTestDetail
)。当我想添加新的LabTestDetail
对象时,我将LabTest ID
(父 ID)传递给Ajax.actionlink
中的LabTestDetail Create action method
,如下所示
@Ajax.ActionLink("+ Add New Lab Test Detail",
"Create", "LabTestDetail",
new { labtestid = Model.LabTestID },
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
HttpMethod = "Get",
UpdateTargetId = "replace",
LoadingElementId = "progress"
})
LabTestDetail
Create Action method
如下所示; 我将labtestid
的值存储在viewbag
中的地方:-
[Authorize(Roles = "Doctor")]
public ActionResult Create(int labtestid)
{
ViewBag.labtestid = labtestid;
LabTestDetail ltr = new LabTestDetail();
return PartialView("_Create", ltr);
}
然后在create view
我将Viewbag
的值存储在hidden html field
上,如下所示:
<input type= "hidden" name = "labtestid" value = @ViewBag.labtestid />
最后LabTestDetail
Post Create action
方法如下所示:-
[Authorize(Roles = "Doctor")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(LabTestDetail ltr, int labtestid)
{
try
{
if (ModelState.IsValid)
{
ltr.LabTestID = labtestid;
repository.AddLabTestDetail(ltr);
repository.Save();
return PartialView("_datails", ltr);
}
}
//code goes here
当我,,,测试它时,上述内容对我来说效果很好,但我最关心的是如何确保labtestid
的值在以下操作期间未被攻击者修改:-
当我将
labtestid
值从ajax.action link
传递到create action method
当我将
labtestid
值作为视图袋传递时最后,当我将
labtestid
值分配给html hidden field
时.
此致敬意提前感谢任何帮助。BR
如果使用身份验证并且只有经过身份验证的用户才能执行这些操作,则可以在应该执行更新的操作中验证当前经过身份验证的用户 ( User.Identity.Name
) 是否是实验室测试的所有者。为此,这意味着您需要将这些实验室测试 ID 与用户相关联。