English 中文(简体)
• 如何防止在我作为p.net MVc网络应用中被指定用于儿童物体的父母物体被编辑
原标题:How can i prevent editing a Parent object if it has been assigned to a child object in my asp.net MVc web application

我有两个目标:

LabTest
LabTestDetails

Where a LabTest object can have zero or many LabTestDetails objects. I need to implement the following business rule:-

用户如果被分配到一个或多个《实验室试验条约》物体,就不应编辑。

(一) 采用一种名为“标签”的助手方法,在“拉贝”物体上(如果把“拉比”物体转让给任何“拉比”物体): ——

public partial class LabTest 
    {    
public bool IsAlreadyAssigned(int id)
            {

                return (LabTestDetailss.Any(r2 => r2.LabTestID == id));

            }}

然后,我增加了对<代码>的如下检查: Get & Post 宣传行动方法:-

  public ActionResult Edit(int id)
    {


        LabTest c = repository.GetLabTest (id);

        if ((c == null) ||  (c.IsAlreadyAssigned (id)))
        {
            return View("Error");
        }

        return View(c);
    }

    [HttpPost]
    public ActionResult Edit(int id, FormCollection colletion)
    {

       LabTest c = repository.GetLabTest (id);
       if ((c == null) ||  (c.IsAlreadyAssigned (id))) // *******
        {
            return View("Error");
       }
        try
        {
            if (TryUpdateModel(c))
            {

                elearningrepository.Save();
                return RedirectToAction("Details", new { id = c.LabTestID });
            }
        }

以上可能会对大多数案件进行罚款,但如果<代码> 试验物体在<代码>后被另一用户指派到实验室试验详细物上(c=无效) 劳动标记((d)) 核对有关上述代码(>>>*<>>>)的员额行动方法,那么我的业务逻辑就会破碎。

so is there a way to implement my action methods so that it will always prevent editing a LabTest object if it has been assigned to a LabTestdetail object .

不丹

最佳回答

各位可以按照评论中的建议使用一种储存的程序,但你也可以创建一种服务方法,以检查是否指定了<条码>。

public bool LabTestIsAssigned(int labTestId)
{
    using (var context = new YourContext())
    {
        return context.LabTestDetails.Any(d => d.LabTestID == id);
    }
}

采用这种方法而不是使用导航财产的好处是保证这一方法能够反映数据库的现状。

请注意,你不得不在挽救变化之前进行这一检查! 即便如此,在评估检查之后,以及在保留改动之前,插入也可能发生。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签