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
Indexview should not haveformelements, just links to anApplymethod (passing theJobIdofJobs) which renders a link to a view to edit. What is the model for your Apply (or Submit?) view?