问题
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!
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
The code for this view:
@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>
}
I can see how I pass my submissionId for my "Edit" link as I am within a loop for the model. This works fine.
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>
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??
回答1:
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);
}
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>
}
回答2:
If your code looks exactly as in your post, then it's a problem that Visual Studio and Razor cannot resolve. You can, though. Your code looks like this:
<p>@Html.ActionLink("Create New", "Create", new {id=Model.[No options appear here!]}</p>
Please check your code once again and proof that the end parenthesis )
is missing. If it is, then simply closing the parenthesis will get you your Intellisense back. That is, your code should look like this:
<p>@Html.ActionLink("Create New", "Create", new {id=Model.[No options appear here!]})</p>
^
look for this _____|
回答3:
I would create a ViewModel instead of using IEnumerable on your cshtml for starters.
View Model
using System.Linq;
using System.Collections.Generic;
public class SubmissionVm {
public IEnumerable<Reports.Models.Submission> submissions { get; set; }
public int MaxId {
return submissions.OrderByDescending(s => s.submissionID).FirstOrDefault().submissionID;
}
}
On your cshtml, in your Create New link you are going after Model., but there are no properties there to look at, because Model is an IEnumerable. Maybe you are trying to get the next id, or the top id in the Enumerable? So the MaxId property above will give you the highest submissionID in your IEnumerable. Then you can modify your Create New link as such.
<p>@Html.ActionLink("Create New", "Create",new {id=Model.MaxId})</p>
On your controller, you will want to maybe increment this number by one, or something to change it from the MaxId
来源:https://stackoverflow.com/questions/25806871/passing-a-parameter-to-html-actionlink-when-the-model-is-ienumerablet