我有两个具有一对多关系的对象 ( LabTest & LabTestDetail)。当我想添加一个新LabTestDetail对象时,我将LabTest ID(父 ID)传递LabTestDetail Create action method给Ajax.actionlink如下
@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 viewi 上存储 a 的值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值分配给 a时html hidden field。
最好的问候提前感谢您的帮助。BR