当模型为IEnumerable< T>时,将参数传递给Html.ActionLink. [英] Passing a parameter to Html.ActionLink when the model is IEnumerable<T>

查看:65
本文介绍了当模型为IEnumerable< T>时,将参数传递给Html.ActionLink.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的希望有人可以帮助我完成这个我想做的简单事情.我对MVC还是很陌生,但仍然想尽一切办法!

I really hope someone can help me with this somewhat simple thing I am trying to do. I am very new to MVC and am still trying to get my head around it!

我有一个名为Submission的控制器,该控制器传递了公司ID,因此我可以跟踪我们在哪个公司工作.

I have a controller called Submission that gets passed a company id so I can keep track of which company we are working in.

http://myurl/Submission/Index/1

此视图的代码:

@model IEnumerable<Reports.Models.Submission>

@{
    ViewBag.Title = "Index";
}

<p>@Html.ActionLink("Create New", "Create")</p>

@foreach (var item in Model)
{
    <p>@Html.DisplayFor(modelItem=>item.ContactName) | @Html.ActionLink("Edit", "Edit", new {      id=item.submissionID }) </p>
}

我可以看到如何在模型循环中传递"Edit"链接的submittingId.效果很好.

I can see how I pass my submissionId for my "Edit" link as I am within a loop for the model. This works fine.

我的问题是我无法在新建" Actionlink中添加参数.

My problem is that I cannot add a parameter to my "Create New" Actionlink e.g.

<p>@Html.ActionLink("Create New", "Create",new {id=Model.[No options appear here!]}</p>

我认为这是因为我的模型顶部被声明为IENumerable,但是我需要像这样的模型来遍历我的公司以进行编辑链接.基本上看起来我需要两个版本的Model,但是我知道这是不可能的.有人知道我可以在这里做什么吗?

I think this is because I have my model declared as IENumerable at the top, but I need it like this to loop through my companies for the edit link. Basically it looks like I need two versions of the Model but I know this is not possible. Anyone know what I can do here??

推荐答案

我假设您有 url/Submission/Index/x ,其中 x 是公司ID,您想要将其生成为创建"链接: url/Submission/Create?CompanyId = x .我建议将模型类更改为如下所示,其中包含 CompanyID Submissions 属性.

I assume when you have url/Submission/Index/x where x is the company id, you want to generate this as the Create link: url/Submission/Create?CompanyId=x. I'd suggest changing the model class to something like below, which contains CompanyID and Submissions property.

public class SubmissionIndexModel
{
    public SubmissionIndexModel()
    {
        this.Submissions = new List<Submission>();
    }

    public int CompanyID { get; set; }
    public List<Submission> Submissions { get; set; }
}

这是您的控制器代码的外观

Here's what your controller code should look like

public ActionResult Index(int id)
{
    SubmissionIndexModel model = new SubmissionIndexModel();
    model.CompanyID = id;
    model.Submissions = (from n in db.Submissions where n.reportNameID == id select n).ToList();

    return View(model);
}

您的视图代码应如下所示,您可以将 Model.CompanyID 传递到创建"操作链接,并通过 Model.Submissions 进行枚举以生成编辑"链接.

Your view code should look like below, you can pass Model.CompanyID to the Create action link and enumerate through Model.Submissions to generate the Edit links.

@model SubmissionIndexModel

@{
    ViewBag.Title = "Index";
}

<p>@Html.ActionLink("Create New", "Create", new { CompanyId = Model.CompanyID })</p>

@foreach (var item in Model.Submissions)
{
    <p>@Html.DisplayFor(modelItem => item.ContactName) | @Html.ActionLink("Edit", "Edit", new { id = item.submissionID }) </p>
}

这篇关于当模型为IEnumerable&lt; T&gt;时,将参数传递给Html.ActionLink.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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