Saturday, August 10, 2019

How to create career page with Upload file (CV) in MVC4

Step - 1: Create New Project.

Go to File > New > Project > Select asp.net MVC4 web application > Entry Application Name > Click OK > Select Internet Application > Select view engine Razor > OK

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for Save data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used one tables as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >


Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Apply validation on model.

Open your model and add validation. Please follow below code

  1. namespace MVCCarrer
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel.DataAnnotations;
  6. public partial class Carrer
  7. {
  8. public int CID { get; set; }
  9. [Required(ErrorMessage="Please provide fullname", AllowEmptyStrings=false)]
  10. public string FullName { get; set; }
  11. public string EmailID { get; set; }
  12. public string CV { get; set; }
  13. }
  14. }

Step-6: Create a Controller.

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add.

Here I have created a controller "CarrerController"

Step-7: Add new action into your controller for Get Method

Here I have added "SubmitCV" Action into "Carrer" Controller. Please write this following code

  1. namespace MVCCarrer.Controllers
  2. {
  3. public class CarrerController : Controller
  4. {
  5. public ActionResult SubmitCV()
  6. {
  7. return View();
  8. }
  9. }
  10. }

Step-8: Add view for your Action & design for create form.

Right Click on Action Method (here right click on SubmitCV action) > Add View... > Enter View Name > Select View Engine (Razor) > Check "Create a strong-typed view" > Select your model class > Add.
HTML Code 
  1. @model MVCCarrer.Carrer
  2. @{
  3. ViewBag.Title = "SubmitCV";
  4. }
  5. <h2>SubmitCV</h2>
  6. @using (Html.BeginForm("SubmitCV", "Carrer", FormMethod.Post, new { enctype="multipart/form-data"}))
  7. {
  8. @Html.ValidationSummary(true)
  9. @Html.AntiForgeryToken()
  10. @* This AntiForgeryTokey is used for prevent CSRF Attack*@
  11. <fieldset>
  12. <legend>Carrer</legend>
  13. @if (ViewBag.Message != null)
  14. {
  15. <span style="color:green">
  16. @ViewBag.Message
  17. </span>
  18. }
  19. <div class="editor-label">
  20. @Html.LabelFor(model => model.FullName)
  21. </div>
  22. <div class="editor-field">
  23. @Html.EditorFor(model => model.FullName)
  24. @Html.ValidationMessageFor(model => model.FullName)
  25. </div>
  26.  
  27. <div class="editor-label">
  28. @Html.LabelFor(model => model.EmailID)
  29. </div>
  30. <div class="editor-field">
  31. @Html.EditorFor(model => model.EmailID)
  32. @Html.ValidationMessageFor(model => model.EmailID)
  33. </div>
  34.  
  35. <div class="editor-label">
  36. Select CV
  37. </div>
  38. <div class="editor-field">
  39. <input type="file" name="file" />
  40. @Html.ValidationMessage("CustomError")
  41. </div>
  42. <p>
  43. <input type="submit" value="Create" />
  44. </p>
  45. </fieldset>
  46. }
  47. <div>
  48. @Html.ActionLink("Back to List", "Index")
  49. </div>
  50.  
  51. @section Scripts {
  52. @Scripts.Render("~/bundles/jqueryval")
  53. }

Step-9: Add new Folder to Save CV

Go to Solution Explorer > Right Click on Solution Explorer > Add > New Folder > Enter folder name.

Step-10: Add new action into your controller for POST Method


Here I have added "SubmitCV" Action with Model Parameter (here "Carrer") into "Carrer" Controller. Please write this following code

  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public ActionResult SubmitCV(Carrer C, HttpPostedFileBase file)
  4. {
  5. if (file == null)
  6. {
  7. ModelState.AddModelError("CustomError", "Please select CV");
  8. return View();
  9. }
  10.  
  11. if (!(file.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ||
  12. file.ContentType == "application/pdf"))
  13. {
  14. ModelState.AddModelError("CustomError", "Only .docx and .pdf file allowed");
  15. return View();
  16. }
  17.  
  18. if (ModelState.IsValid)
  19. {
  20. try
  21. {
  22. string fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
  23. file.SaveAs(Path.Combine(Server.MapPath("~/UploadedCV"), fileName));
  24. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  25. {
  26. C.CV = fileName;
  27. dc.Carrers.Add(C);
  28. dc.SaveChanges();
  29. }
  30. ModelState.Clear();
  31. C = null;
  32. ViewBag.Message = "Successfully Done";
  33. }
  34. catch (Exception ex)
  35. {
  36. ViewBag.Message = "Error! Please try again";
  37. return View();
  38. }
  39. }
  40. return View();
  41. }

Step-11: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...