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

MVC C# foreach Loop passing button selection

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

这是我的 控制器 class。 我提取了索引中的作业,单击按钮后我想转到 "Apply" 方法

public ActionResult Index()
{
  var jobs = (from Jobs in db.Jobs
              orderby Jobs.Id descending
              select Jobs);
  List<CareersClasses.Jobs> job = jobs.ToList();
  return View(job);
}

[HttpPost]
public ActionResult Apply(){
  return View();
}

这是 索引 视图:

<table>
  @using (Html.BeginForm("Apply", "Jobs", FormMethod.Post))
  {
    foreach (var item in Model)
    {
      <tr>
        <td>
          @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
          @Html.DisplayFor(modelItem => item.Desc)
        </td>
        <td>
          <button id="Apply" name="Apply" value="@item.Title">Apply</button>
        </td>
      </tr>
    }
  }
</table>

这是应用视图

@using (Html.BeginForm("Submit", "Jobs", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <fieldset>
    <legend>Applicants</legend>
    <div class="editor-label">
      @Html.LabelFor(model => model.JobId)
    </div>
    <div class="editor-field">
      @Html.DisplayFor(model=> model.JobId)
    </div>
    <div class="editor-label">
      @Html.LabelFor(model => model.FName)
    </div>
    <div class="editor-field">
      @Html.EditorFor(model => model.FName)
      @Html.ValidationMessageFor(model => model.FName)
    </div>
    <div class="editor-label">
      @Html.LabelFor(model => model.LName)
    </div>
    <div class="editor-field">
      @Html.EditorFor(model => model.LName)
      @Html.ValidationMessageFor(model => model.LName)
    </div>
    <div class="editor-label">
      @Html.LabelFor(model => model.Email)
    </div>
    <div class="editor-field">
      @Html.EditorFor(model => model.Email)
      @Html.ValidationMessageFor(model => model.Email)
    </div>
    <div class="editor-label">
      @Html.LabelFor(model => model.PhoneNb)
    </div>
    <div class="editor-field">
      @Html.EditorFor(model => model.PhoneNb)
      @Html.ValidationMessageFor(model => model.PhoneNb)
    </div>
    <div class="editor-label">
      @Html.LabelFor(model => model.Country)
    </div>
    <div class="editor-field">
      @Html.EditorFor(model => model.Country)
      @Html.ValidationMessageFor(model => model.Country)
    </div>
    <div class="editor-label">
      Curriculum Vitae
    </div>
    <div class="editor-field">
      <input type="file" name="file"/> 
    </div>
    <div class="editor-label">
      @Html.EditorFor(model => model.JobId)
    </div>
    <p>
      <input type="submit" value="Create" />
    </p>
  </fieldset>
}

我还想将文档添加到我的数据库中: 我在"submit"方法中使用了这个方法是否正确?

提交方式:

[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 还是个新手,在问这些问题之前我做了很多研究,但我仍然迷路了,所以非常感谢你的帮助

Index 视图应该有 links(不是表单)到作业的 Apply() 方法。 link 将 JobsId 传递给方法,该方法初始化 CareersClasses 的新实例并设置其 JobId 属性。该方法然后 returns 一个视图来编辑 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>

控制器

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
}

Apply.cshtml

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