在ASP.NET MVC中传递父对象之间的值 [英] Pass values between parent to child in ASP.NET MVC

查看:59
本文介绍了在ASP.NET MVC中传递父对象之间的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将父ID传递给子控制器?我是用Viewbag做的还是其他方式。每个行的索引视图上都有一个链接,它应该通过控制器打开子视图索引。该怎么办?



父母索引查看有一个链接:



How do you pass a parent ID to a child controller? Do I do this with Viewbag or is there another way. There is a link on the Index view for each row which should open the child view Index through the controller. How should this be done?

Parent Index View has a link:

@Html.ActionLink("Tensile", "Index","ToleranceTensile", new { id=item.ToleranceID }, null)





孩子的控制器如何显示父母ID的索引视图?







How should the controller on the child display the Index view for thAt parent ID?


public ActionResult Index(int id)
       {
           ViewBag.ToleranceHeaderID = id;
           return View(toleranceTensileRepository.GetToleranceTensilesByHeaderID(id));
       }



同样如何传递该值以创建子视图,以便将任何新记录作为子项添加到父级最初传递给子索引?


As well how can that value be passed to create view for child so any new records are added as children to the parent which was originally passed to child Index?

推荐答案

最后一句是关键。但它被称为比父母更多的主要细节...

所以你想要选择一个主实体,而不是在以下几个细节动作调用中使用该实体。

ViewBag用于将非模型数据从控制器传递到视图,所以不是它。

你有几个选择:

1)cookie:在主选择之后你发送一个包含主id的结果的cookie,你在以下调用中查找它

2)隐藏字段:如果你使用表单,你可以添加一个隐藏存储主实体ID的字段,您可以在详细操作中使用它。

3)TempData:您可以在调用之间传递数据,但只有一跳,所以如果用户跳过网站,它不会工作。

4)会话:就是这样。在主实体选择中,您将主实体(或它的id)存储在会话变量中,您可以在所有后续调用中使用它,直到会话被放弃,超时或您处置变量。请注意,在控制器构造函数中,会话尚未构建,因此您只能在操作中访问它。
The last sentence is the key. But it is called more master-detail than parent-child...
So you want to select a "master" entity and than use that entity in several following detail action calls.
ViewBag is for passing non-model data from controller to the view, so that''s not it.
You have several options:
1) cookie: on after master selection you send a cookie with the result that contains the master id, and you look for it in in the following calls
2) hidden field: if you use forms, you can add a hidden field that stores the master entity id, and you can use it in the detail actions.
3) TempData: you can pass data between calls but only for one hop, so if the user is jumping around the site, it won''t work.
4) session: this is it. On master entity selection you store the master entity (or it''s id) in a session variable and you can use it in all subsequent calls, until session is abandoned, timed out, or you dispose the variable. Be aware, that in the controller constructor the session is not yet constructed, so you can access it only in the actions.


您好,

甚至更简单只需使用下面的actionlink



Hi,
and even more simply just use your actionlink like below

<%:Html.ActionLink("Text To display", "Index", new {  }, new { href = "/Parent/Child/" })%>





或者你可以添加一个javascript函数来调用你的行动,如:



or you can add a javascript function to call your action like:

<%:Html.ActionLink("Text To display", "Index", new { id = 3 }, new {  onclick = "return TestAction('CreatedOn');" })%>


这篇关于在ASP.NET MVC中传递父对象之间的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆