@ Html.ActionLink可以发送ID,并在其他视图中查看ID

时间:2018-06-23 19:09:36

标签: asp.net-mvc razor

我想去这个地方。在项目中,我们首先创建一个调查。然后单击调查旁边的按钮以查看调查的问题。

Survey's List. And "Soruları Gör" is that button.

单击按钮后,我们转到另一个控制器的索引页面。像这样, Survey's Questions' Index Page. Click on the button in the box to add a new question to the survey.

我想将调查的ID发送到新的问题页面并在该页面上显示。 new question creation page

我希望调查的ID出现在图片3的红色框中。

This view belongs to the 2nd picture. 

@model IEnumerable<PerformansTakipAnket.Models.View_Base_UyeAnketSorular_Getir>

        @{
            ViewBag.Title = "Index";
        }

        <h2>Index</h2>

        <p>

            @Html.ActionLink("Yeni Soru", "Create")

        </p>
        <table class="table">
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.UyeAnketID)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.uyeAnketSoruID)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.UyeAnketSoruTip)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.UyeAnketSoruUstID)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.uyeAnketSoruBaslik)
                </th>
                <th></th>
            </tr>

        @foreach (var item in Model) {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.UyeAnketID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.uyeAnketSoruID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.UyeAnketSoruTip)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.UyeAnketSoruUstID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.uyeAnketSoruBaslik)
                </td>
                <td>
                    @Html.ActionLink("Cevapları Gör", "Index", "Cevap", new { id = item.uyeAnketSoruID }, null) |
                    @Html.ActionLink("Details", "Details", new { id = item.uyeAnketSoruID }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.uyeAnketSoruID })


                </td>
            </tr>
        }

        </table>

public ActionResult Index(int id)
        {
            var sorular = db.View_Base_UyeAnketSorular_Getir.Where(i => i.UyeAnketID == id);
            ViewBag.UyeAnketID = id;

            return View(sorular.ToList());
        }

// GET: Soru/Create
        public ActionResult Create(int id)
        {


            @ViewBag.UyeAnketID = id;


            return View();
        }

        // POST: Soru/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "uyeAnketSoruID,UyeAnketID,UyeAnketSoruTip,UyeAnketSoruUstID,uyeAnketSoruBaslik")] View_Base_UyeAnketSorular_Getir view_Base_UyeAnketSorular_Getir)
        {
            if (ModelState.IsValid)
            {

                db.Proc_UyeAnketSorular_EkleGuncelleSil("YeniKayit", view_Base_UyeAnketSorular_Getir.uyeAnketSoruID, view_Base_UyeAnketSorular_Getir.UyeAnketID, view_Base_UyeAnketSorular_Getir.UyeAnketSoruTip, view_Base_UyeAnketSorular_Getir.UyeAnketSoruUstID, view_Base_UyeAnketSorular_Getir.uyeAnketSoruBaslik);
                db.SaveChanges();
                return RedirectToAction("Index", new { @id = view_Base_UyeAnketSorular_Getir.UyeAnketID });
            }

            return View(view_Base_UyeAnketSorular_Getir);
        }

希望我能告诉你问题所在。

1 个答案:

答案 0 :(得分:0)

您必须将SurveyId从操作方法传递到问题列表视图,并在呈现“创建”链接时使用该ID来将SurveyId添加为查询字符串。

看起来您没有视图模型,在这种情况下,您可以使用ViewBag传递此数据。

public ActionResult Index(int id)
{ 
   var questions = db.Questions.Where(a=>a.SurveyId==id).ToList();
   ViewBag.SurveyId = id;
   return View(questions);
}

现在,在您的视图中,您可以使用此ViewBag项值来填充ActionLink辅助方法的RouteValueDictionary参数。

@Html.ActionLink("Yeni Soru", "Create",new { surveyId= ViewBag.SurveyId })

假设您的Create操作方法具有一个名为surveyId的参数

public ActionResult Create(int surveyId)
{
  // Use surveyId as needed ( Pass to view via ViewBag/ViewModel)
  // to do :return something
}

我还建议研究视图模型,而不是通过ViewBag / ViewData传递数据。恕我直言,这更是一种干净的方法。