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

本文关键字:按钮 选择 C#foreach 循环 MVC | 更新日期: 2024-09-24 04:39:59

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

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

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

我还想将一个文档添加到我的数据库中:我在"提交"方法中使用了这种方法,对吗?

提交方法:

[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的新手,在问这些问题之前我做了很多研究,但我仍然迷失了方向,所以我们将非常感谢您的帮助

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

Index视图应该具有指向作业的Apply()方法的链接(而不是表单)。链接将JobsId传递给方法,该方法初始化CareersClasses的新实例并设置其JobId属性。然后,该方法返回一个视图来编辑CareersClasses,提交按钮将模型发布回POST方法。

索引.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" />
}