【发布时间】:2016-05-07 12:18:47
【问题描述】:
在我的 ASP.NET MVC 5 应用程序中,我有以下项目的索引视图(从数据库中的 Projects 表中检索)。
@using Leepio.Models
@using Microsoft.AspNet.Identity
@model ApplicationTwoViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create" ,"Projects")
</p>
<table class="table">
<tr>
<th>
@Html.ActionLink("Title", "Index", new {SortOrder = (ViewBag.SortOrder==null?"Asc":(ViewBag.SortOrder=="Asc"?"Desc":"Asc")), SortBy = "Title"})
</th>
<th>
@Html.ActionLink("Application Deadline", "Index", new {SortOrder = (ViewBag.SortOrder == null ? "Asc" : (ViewBag.SortOrder == "Asc" ? "Desc" : "Asc")), SortBy = "ApplicationDeadline"}) <br/>
@Html.ActionLink("Hourly Rate (DKK)", "Index", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : (ViewBag.SortOrder == "Asc" ? "Desc" : "Asc")), SortBy = "HourlyRate" })
</th>
<th>
@Html.ActionLink("Skill requirements", "Index", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : (ViewBag.SortOrder == "Asc" ? "Desc" : "Asc")), SortBy = "RequiredSkills" })
</th>
</tr>
@foreach (var item in Model.Model1) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ApplicationDeadline)<br/>
@Html.DisplayFor(modelItem => item.HourlyRate)
</td>
<td>
@Html.DisplayFor(modelItem => item.RequiredSkills)
</td>
<td>
@if(item.UserId == User.Identity.GetUserId())
{
@Html.ActionLink("Edit", "Edit", new {id = item.ProjectId})
@Html.ActionLink("Delete", "Delete", new {id = item.ProjectId})
}
@Html.ActionLink("Details", "Details", new {id = item.ProjectId}) |
@Html.ActionLink("Apply", "Create", "Applications" , new { id = Model.Model2.ApplicationId }) |
</td>
</tr>
}
</table>
@{
double TotalPage = @ViewBag.TotalPages;
}
<ul class="pagination">
@for (int i = 1; i <= TotalPage; i++)
{
if (i == ViewBag.Page)
{
<li class="active"> @Html.ActionLink(i.ToString() + " ", "Index", "Projects", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : ViewBag.SortOrder), SortBy = (ViewBag.SortBy == null ? "Title" : ViewBag.SortBy), Page = i })</li>
}
else
{
<li>
@Html.ActionLink(i.ToString() + " ", "Index", "Projects", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : ViewBag.SortOrder), SortBy = (ViewBag.SortBy == null ? "Title" : ViewBag.SortBy), Page = i })
</li>
}
}
</ul>
我正在使用我创建的 ApplicationTwoViewModel,它基本上有两个视图模型:
public class ApplicationTwoViewModel
{
public IEnumerable<Project> Model1 { get; set; }
public Application Model2 { get; set; }
}
我正在尝试制作这个 ActionLink
@Html.ActionLink("Apply", "Create", "Applications" , new { id = Model.Model2.ApplicationId }) |
从 ApplicationsController 中的 Create ActionRestult 创建一个新的“应用程序”:
public ActionResult Create()
{
return View();
}
// POST: Applications/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ApplicationId,ProjectId,UserId,CoverLetter")] Application application)
{
if (ModelState.IsValid)
{
db.Applications.Add(application);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(application);
应用模型:
public class Application
{
public int ApplicationId { get; set; }
public int ProjectId { get; set; }
public string UserId { get; set; }
public string CoverLetter { get; set; }
}
所以现在我正在尝试创建“应用”ActionLink,以便创建视图从项目索引视图接收 ProjectId,就像编辑/删除/详细信息接收 ProjectId 一样,我不确定如何执行此操作。
现在它可以工作,但您必须手动插入 ProjectId 和 UserId(登录的应该是 User.Identity.GetUserId() 但我不确定在这种情况下在哪里添加它),我想要它们在幕后被检索,因此用户只需编写 CoverLetter 然后创建应用程序。
【问题讨论】:
标签: c# asp.net-mvc razor asp.net-mvc-5 html.actionlink