MVC C#foreach循环传递按钮选择

MVC C#foreach循环传递按钮选择,c#,asp.net-mvc,entity-framework-migrations,C#,Asp.net Mvc,Entity Framework Migrations,我有一个列表视图,显示从数据库中提取的作业。每个作业旁边都有一个按钮。当我移动到下一页时,我需要携带指定的作业,显示它,然后将其保存到我的数据库中 这在我的控制器类中。 我提取了索引中的作业,单击按钮后,我想转到“应用”方法 课程模型: [HttpPost] public ActionResult Submit(FormCollection formCollection, HttpPostedFileBase file) { CareersClasses.Applicants Appli

我有一个列表视图,显示从数据库中提取的作业。每个作业旁边都有一个按钮。当我移动到下一页时,我需要携带指定的作业,显示它,然后将其保存到我的数据库中

这在我的控制器类中。 我提取了索引中的作业,单击按钮后,我想转到“应用”方法

课程模型:

[HttpPost]
public ActionResult Submit(FormCollection formCollection, HttpPostedFileBase file)
{
    CareersClasses.Applicants Applicants = new CareersClasses.Applicants();
    if (ModelState.IsValid)
    {

        Applicants.FName = formCollection["FName"];
        Applicants.LName = formCollection["LName"];
        Applicants.Email = formCollection["Email"];
        Applicants.Country = formCollection["Country"];
        Applicants.PhoneNb = int.Parse(formCollection["PhoneNb"]);

        if (file != null && file.ContentLength > 0)
        {
            byte[] data = GetDocument(file);

            Applicants.CV = data;
        }

        db.Applicants.Add(Applicants);
        db.SaveChanges();
        return RedirectToAction("ListApps");
    }
    return View();
}

[HttpPost]
public byte[] GetDocument(HttpPostedFileBase file)
{
    //Get file info
    var fileName = Path.GetFileName(file.FileName);
    var contentLength = file.ContentLength;
    var contentType = file.ContentType;

    //Get file data
    byte[] data = new byte[] { };
    using (var binaryReader = new BinaryReader(file.InputStream))
    {
        data = binaryReader.ReadBytes(file.ContentLength);
    }

    return data;

}
public class CareersClasses
    {
        public class Applicants
        {
            [Key]
            public int Id { get; set; }
            public string FName { get; set; }
            public string LName { get; set; }
            public int PhoneNb { get; set; }
            public string Email { get; set; }
            public string Country { get; set; }
            public byte[] CV { get; set; }

            public int JobId { get; set; }
            [ForeignKey("JobId")]
            public virtual Jobs Jobs { get; set; }

        }

        public class Jobs
        {
            [Key]
            public int Id { get; set; }
            public string Title { get; set; }
            public string Desc { get; set; }
        }

    }

注意:我还是MVC新手,在问这些问题之前我做了很多研究,但我还是迷路了,因此非常感谢您的帮助索引视图应该有链接(而不是表单)指向工作的
应用()
方法。该链接将
Jobs
Id
传递给方法,该方法初始化
CareersClasses
的新实例并设置其
JobId
属性。然后,该方法返回一个视图来编辑
CareersClasses
,然后“提交”按钮将模型发回POST方法

Index.cshtml

<table>
  @foreach (var item in Model)
  {
    <tr>
      <td>@Html.DisplayFor(modelItem => item.Title)</td>
      <td>@Html.DisplayFor(modelItem => item.Desc)</td>
      <td>@Html.ActionLink("Apply", "Apply", new { ID = item.Id})</td>
    </tr>
  }
</table>
Apply.cshtml

@model CareersClasses
....
@Html.BeginForm())
{
  // controls for properties of CareersClasses
  ....
  <input type="submit" value="Create" />
}
@model CareersClasses
....
@Html.BeginForm())
{
//CareerClass属性的控件
....
}

很难准确理解您在这里做什么。您的
索引
视图不应包含
表单
元素,只应包含指向
应用
方法的链接(传递
作业
作业ID
),该方法将呈现指向要编辑的视图的链接。应用(或提交)视图的模型是什么?@StephenMuecke我将表单元素放在索引视图中只是为了节省访问时间。。这只是一个练习mvc的小项目,这就是为什么。。。我将把模型包括在一个列表中second@StephenMuecke我的步骤是:1-第一页(索引)列出了职务和说明,每个职务和说明组合都有一个应用按钮。2-单击其中一个按钮后,用户将被移动到“应用”页面,其中有一个标题,注明他/她申请的职位,以及一个要填写信息的文本框列表
public class CareersClasses
    {
        public class Applicants
        {
            [Key]
            public int Id { get; set; }
            public string FName { get; set; }
            public string LName { get; set; }
            public int PhoneNb { get; set; }
            public string Email { get; set; }
            public string Country { get; set; }
            public byte[] CV { get; set; }

            public int JobId { get; set; }
            [ForeignKey("JobId")]
            public virtual Jobs Jobs { get; set; }

        }

        public class Jobs
        {
            [Key]
            public int Id { get; set; }
            public string Title { get; set; }
            public string Desc { get; set; }
        }

    }
<table>
  @foreach (var item in Model)
  {
    <tr>
      <td>@Html.DisplayFor(modelItem => item.Title)</td>
      <td>@Html.DisplayFor(modelItem => item.Desc)</td>
      <td>@Html.ActionLink("Apply", "Apply", new { ID = item.Id})</td>
    </tr>
  }
</table>
public ActionResult Apply(int ID)
{
  CareersClasses model = new CareersClasses();
  model.JobID = ID;
  return View(model);
}

[HttpPost]
public ActionResult Apply(CareersClasses model, HttpPostedFileBase file)
{
  // save the model and redirect
}
@model CareersClasses
....
@Html.BeginForm())
{
  // controls for properties of CareersClasses
  ....
  <input type="submit" value="Create" />
}