在具有 "dual" 视图模型时使用 HTML ActionLink 路由参数

Using an HTML ActionLink route argument when having a "dual" view model

在我的 ASP.NET MVC 5 应用程序中,我有以下项目索引视图(从数据库中的项目 table 中检索)。

@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 ActionResult 创建一个新的 "Application":

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; }
}

所以现在我正在尝试创建 "Apply" ActionLink,以便 Create 视图从项目索引视图接收 ProjectId,就像 Edit/Delete/Details 接收 ProjectId 一样,但我不确定该怎么做这个。

现在它可以工作,但你必须手动插入 ProjectId 和 UserId(登录的应该是 User.Identity.GetUserId() 但我不确定在这种情况下在哪里添加它),并且我希望在后台检索它们,这样用户只需编写 CoverLetter,然后创建应用程序。

您需要在 Create() 方法中添加一个参数并使用该值初始化您的模型并将其传递给视图

public ActionResult Create(int projectId)
{
    Application model = new Application()
    {
        ProjectId = projectId 
    };
    return View(model);
}

并修改你 link 以传递值(目前它添加值作为 html 属性,而不是路由值(注意第 5 个参数)

@Html.ActionLink("Apply", "Create", "Applications" , new { projectId = item.ProjectId }, null)

然后在视图中,包含 ProjectId 的隐藏输入,以便它提交给 POST 值

@Html.HiddenFor(m => m.ProjectId)

请注意,您的 UserID 值应在保存 Application 之前添加到 POST 方法中(不在模型或视图中)

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ProjectId, CoverLetter")] Application application)
{
    if (ModelState.IsValid)
    {
        applicatio.UserId = User.Identity.GetUserId(); // set the user here
        db.Applications.Add(application);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(application);
}

旁注:您的 ApplicationTwoViewModel 模型的用途尚不清楚。您在视图中使用的所有内容都是 IEnumerable<Project>,因此您的 Model2 属性 似乎是不必要的