1

I have a listing view which shows the jobs extracted from the database. Each Job has a button next to it. When I move to the next page, i need to carry the specified job, display it and later save it to my database.

This is inside my Controller class. I extracted the jobs in the index and after clicking the button i want to move to the "Apply" method

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

This is The Index 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>

This is the Apply View

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

I also want to add a document to my database: I used this method in the "submit" method is it correct?

Submit Method:

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

}

The Classes Model:

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

    }

Note: I am still new to MVC and I did a lot of research before asking these questions but i'm still lost, so your help will be much appreciated

3
  • Difficult to understand exactly what your doing here. Your Index view should not have form elements, just links to an Apply method (passing the JobId of Jobs) which renders a link to a view to edit. What is the model for your Apply (or Submit?) view? Commented Mar 1, 2015 at 23:41
  • @StephenMuecke I put the form elements in the Index view just to save accessing time.. this is just a small project to practice mvc that's why... I will include the models in a second Commented Mar 1, 2015 at 23:45
  • @StephenMuecke my steps are: 1-the first page(index) lists the job Titles and description with an apply button for each job title and description combinationr 2-after clicking one of the buttons, the user is moved to the apply page where there's a title noting what job title he/she applied to and a list of textboxes to fill information Commented Mar 1, 2015 at 23:50

1 Answer 1

1

The Index view should have links (not forms) to an Apply() method for the Job. The link passes the Id of the Jobs to the method, which initializes a new instance of CareersClasses and sets its JobId property. The method then returns a view to edit the CareersClasses and the submit button posts back the model to the POST method.

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>

Controller

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" />
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.