0

This is my code,i have also tried strongly typed view but i it does not create an upload field

   #Model
   namespace master_layout.Models

   {
    [Table("Candidates")]
    public class Candidate
    {

    [Key]

            public int CandidateID { get; set; }
    public string FirstName { get; set; }
    public string LastName{get;set;}
    public DateTime Date{get;set;}
    public string EmailID{get;set;}
    public long ContactNumber{get;set;}
    public long AlternateContactNumber{get;set;}
    public int RecruitmentStatusID{get;set;}
    public DateTime CreatedOn{get;set;}
    public DateTime LastUpdatedOn{get;set;}
    public byte[] Resume { get; set; }
}
public class CandidateContext : DbContext
{
    public DbSet<Candidate> Candidates { get; set; }

}


The following is my controller code

 Controller:

namespace proedit1.Controllers
{
public class HomeController : Controller
{

    CandidateContext db = new CandidateContext();
    public ActionResult Index()
    {


        return View(db.Candidates.ToList());
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Candidate candidate)
    {
        if (ModelState.IsValid)
        {
            db.Candidates.Add(candidate);
            db.SaveChanges();

            return RedirectToAction("Index", "Home");
        }
        return View(candidate);
    }
}
}


View:

@using (Html.BeginForm("Newcandidate", "CandidatesController", FormMethod.Post, new {      EncType="multipart/form-data"}))
{
<table class="navbar-form">

<tr>
<th>First Name<input type="text" placeholder="Your First  Name" class="margin"/></th>
</tr>
<tr>
<th>Last  Name<input type="text" placeholder="Your  Last Name"/></th>
</tr>
<tr>
<th>Date of Birth&nbsp;<input type="date" name="db" /></th>

</tr>
<tr>
 <th>E-Mail ID<input  type="email" placeholder="abc@yourserviceprovider.com" /></th>
</tr>
 <tr>
  <th>Contact No<input type="tel" placeholder="04426506555    or 98414981414" min="8" maxlength="10"/></th>
 <tr>
 <tr>
 <th>Alternate No&nbsp;<input type="tel" placeholder="04426506555 or 98414981414" min="8" maxlength="10"/></th>
  <tr>
  <th>Created On&nbsp;<input type="date" /></th>

  <tr>
  <th>Updated On&nbsp;&nbsp;<input type="date" /></th>

  </tr>

  <tr>
  <th> <input type="file" placeholder="your resume in ms.word format" accept="application/msword" name="NamebtnUpload"/>    
  </th></tr>
  </table>

  <div class="row">

 <div class="span4">

    <input type="submit" class="btn-success" value="UPLOAD" name="Submit"/>
    <input type="reset" class="btn-warning"    name="Reset" />    

</div>
</div>
}

I am a newbie in MVC pls provide me with the correct code to upload details to my database

Jerry
  • 114
  • 2
  • 12
  • *sidenote:* never use ` ` to align your HTML. Use CSS instead. – Raptor Feb 25 '14 at 10:37
  • _sidenote #2:_ you've also added ` ` to the `name` attribute of your `submit` and `reset` buttons but the name values aren't for display. [I don't think this causes your problem though.] – richaux Feb 25 '14 at 10:58

1 Answers1

0

There is a solution which are you looking for:

MVC 4 Razor File Upload

Create new view model for your 'view'. Don't use entity model, please.

Community
  • 1
  • 1
melvas
  • 2,346
  • 25
  • 29