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

时间:2015-03-01 23:19:07

标签: c# asp.net-mvc ef-migrations

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

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

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

我还想在我的数据库中添加一个文档: 我在&#34;提交&#34;中使用了这种方法。方法是否正确?

提交方法:

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

1 个答案:

答案 0 :(得分:1)

Index视图应该包含作业的Apply()方法的链接(不是表单)。该链接将Id的{​​{1}}传递给该方法,该方法初始化Jobs的新实例并设置其CareersClasses属性。然后该方法返回一个视图以编辑JobId,并且提交按钮将模型回发给POST方法。

Index.cshtml

CareersClasses

控制器

<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

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
}