0

나는 VS 2013에 있으며 일반적으로 ASP.NET MVC를 완전히 처음 사용합니다. 나는 행운이없는 언젠가 이것을하려고 시도하고있다. 사용자가 이미지 업로드로 양식을 작성하게하고 이미지가 DB에 작성된 양식을 갖도록하십시오. DB에 경로를 작성하고 응용 프로그램의 내용 폴더에 이미지를 저장하는 것이 가장 좋지만이 작업을 수행하는 방법을 모릅니다. 누군가 나에게 출발점을 줄 수 있습니까?뷰, 뷰 모델 및 컨트롤러를 사용하여 SQL 서버에 이미지 업로드

또한 드롭 다운을 채우는 데 사용되는 ClientUtility.cs이 있지만 지금 당장 필요하지는 않습니다.

현재 db의 이미지 참조에 대한 데이터 유형은 VARCHAR(255)입니다.

Controller

:

private TpsEntities db = new TpsEntities(); 
    [HttpPost] 
    public ActionResult SaveStaffDetails(RegisterStaffViewModel model) 
    { 

     var staff = new staffTable() 
     { 
      staffFirstName = model.FirstName, 
      staffLastName = model.LastName, 
      staffTitle = model.SelectedTitle, 
      staffAddress = model.Address, 
      staffCity = model.City, 
      staffState = model.SelectedState, 
      staffZip = model.ZipCode, 
      staffExperience = model.SelectedExperience, 
      staffEducation = model.SelectedEducation, 
      desiredSalary = model.SelectedSalary, 
      staffProfession = model.SelectedProfession, 
      staffAvailibity = model.SelectedAvailability, 
      staffEmail = model.EmailAddress, 
      staffPhoneNum = model.PhoneNumber, 
      staffPhoto = null, 
      userID = model.UserId 
     }; 

     using (var db = new TpsEntities()) 
     { 
      db.staffTables.Add(staff); 
      db.SaveChanges(); 
     } 

     var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
     var user = userManager.FindById(model.UserId); 

     userManager.AddToRole(model.UserId, "Staff"); 

     ClaimsIdentity identity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); 
     identity.AddClaim(new Claim(ClaimTypes.Role, "Staff")); 


     var AuthenticationManager = System.Web.HttpContext.Current.GetOwinContext().Authentication; 

     AuthenticationManager.SignIn(new AuthenticationProperties() {IsPersistent = false}, identity); 

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

ViewModel :

public class RegisterStaffViewModel 
{ 
    [Required] 
    [Display(Name = "First Name")] 
    public string FirstName { get; set; } 


    [Required] 
    [Display(Name = "Last Name")] 
    public string LastName { get; set; } 

    [Required] 
    [Display(Name = "Address")] 
    public string Address { get; set; } 

    public System.Web.UI.WebControls.ListItemCollection Title { get; set; } 
    [Required] 
    [Display(Name = "Title")] 
    public string SelectedTitle { get; set; } 

    [Required] 
    [Display(Name = "City")] 
    public string City { get; set; } 

    public System.Web.UI.WebControls.ListItemCollection States { get; set; } 
    [Required] 
    [Display(Name = "State")] 
    public string SelectedState { get; set; } 

    [Required] 
    [Display(Name = "Zip Code")] 
    public double? ZipCode { get; set; } 

    [Required] 
    [Phone] 
    [Display(Name = "Phone Number")] 
    public string PhoneNumber { get; set; } 

    [Required] 
    [EmailAddress] 
    [Display(Name = "Email Address")] 
    public string EmailAddress { get; set; } 

    public System.Web.UI.WebControls.ListItemCollection Experience { get; set; } 
    [Required] 
    [Display(Name = "Experience")] 
    public string SelectedExperience { get; set; } 

    public System.Web.UI.WebControls.ListItemCollection Education { get; set; } 
    [Required] 
    [Display(Name = "Education")] 
    public string SelectedEducation { get; set; } 

    public System.Web.UI.WebControls.ListItemCollection Salary { get; set; } 
    [Required] 
    [Display(Name = "Salary")] 
    public string SelectedSalary { get; set; } 

    public System.Web.UI.WebControls.ListItemCollection Profession { get; set; } 
    [Required] 
    [Display(Name = "Profession")] 
    public string SelectedProfession { get; set; } 

    public System.Web.UI.WebControls.ListItemCollection Availability { get; set; } 
    [Required] 
    [Display(Name = "Availability")] 
    public string SelectedAvailability { get; set; } 

    public string UserId { get; set; } 
} 

View :

@*Start of Registration Form for Staff*@ 

@using (Html.BeginForm("SaveStaffDetails", "Staff", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
{ 
      @Html.AntiForgeryToken() 
      @Html.HiddenFor(m => m.UserId) 
      <fieldset> 

       <!-- Form Name --> 
       <legend>Register New Staff</legend> 

       @*First Name*@ 
       <div class="form-group"> 
        <div class="col-md-6"> 
         @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control input-md", @placeholder = "First Name" }) 
        </div> 
       </div> 

       @*Last Name*@ 
       <div class="form-group"> 
        <div class="col-md-6"> 
         @Html.TextBoxFor(m => m.LastName, new { @class = "form-control input-md", @placeholder = "Last Name" }) 
        </div> 
       </div> 

       @*Person Title*@ 
       <div class="form-group"> 
        <label class="col-md-6 control-label" for="title">Title</label> 
        <div class="col-md-6"> 
         @Html.DropDownListFor(m => m.SelectedTitle, new SelectList(Model.Title, "Text", "Value")) 
        </div> 
       </div> 


       @*Address Line*@ 
       <div class="form-group"> 
        <div class="col-md-6"> 
         @Html.TextBoxFor(m => m.Address, new { @class = "form-control input-md", @placeholder = "Address" }) 
        </div> 
       </div> 

       @*City*@ 
       <div class="form-group"> 
        <div class="col-md-6"> 
         @Html.TextBoxFor(m => m.City, new { @class = "form-control input-md", @placeholder = "City" }) 
        </div> 
       </div> 

       @*State*@ 
       <div class="form-group"> 
        <label class="col-md-6 control-label" for="state">State</label> 
        <div class="col-md-6"> 
         @Html.DropDownListFor(m => m.SelectedState, new SelectList(Model.States, "Text", "Value")) 
        </div> 
       </div> 

       @*Zip Code*@ 
       <div class="form-group"> 
        <div class="col-md-6"> 
         @Html.TextBoxFor(m => m.ZipCode, new { @class = "form-control input-md", @placeholder = "Zip Code" }) 
        </div> 
       </div> 

       @*Phone Number*@ 
       <div class="form-group"> 
        <div class="col-md-6"> 
         @Html.TextBoxFor(m => m.PhoneNumber, new { @class = "form-control input-md", @placeholder = "Phone Number" }) 
        </div> 
       </div> 

       @*Email Address*@ 
       <div class="form-group"> 
        <div class="col-md-6"> 
         @Html.TextBoxFor(m => m.EmailAddress, new { @class = "form-control input-md", @placeholder = "Email Address" }) 
        </div> 
       </div> 

       @*Experience*@ 
       <div class="form-group"> 
        <label class="col-md-6 control-label" for="experience">Experience</label> 
        <div class="col-md-6"> 
         @Html.DropDownListFor(m => m.SelectedExperience, new SelectList(Model.Experience, "Text", "Value")) 
        </div> 
       </div> 

       @*Education*@ 
       <div class="form-group"> 
        <label class="col-md-6 control-label" for="education">Education</label> 
        <div class="col-md-6"> 
         @Html.DropDownListFor(m => m.SelectedEducation, new SelectList(Model.Education, "Text", "Value")) 
        </div> 
       </div> 

       @*Desired Salary*@ 
       <div class="form-group"> 
        <label class="col-md-6 control-label" for="salary">Desired Salary</label> 
        <div class="col-md-6"> 
         @Html.DropDownListFor(m => m.SelectedSalary, new SelectList(Model.Salary, "Text", "Value")) 
        </div> 
       </div> 

       @*Profession*@ 
       <div class="form-group"> 
        <label class="col-md-6 control-label" for="profession">Profession</label> 
        <div class="col-md-6"> 
         @Html.DropDownListFor(m => m.SelectedProfession, new SelectList(Model.Profession, "Text", "Value")) 
        </div> 
       </div> 

       @*Availability*@ 
       <div class="form-group"> 
        <label class="col-md-6 control-label" for="availability">Availability</label> 
        <div class="col-md-6"> 
         @Html.DropDownListFor(m => m.SelectedAvailability, new SelectList(Model.Availability, "Text", "Value")) 
        </div> 
       </div> 

       <!-- INSERT IMAGE UPLOAD HERE --> 
      </fieldset> 

      <input type="submit" value="Save" /> 
} 

답변

0

편집은 다음 code.I 같은 컨트롤러는 staffPhoto가 picturepath하고 있음을 가정한다. 보기에

public ActionResult SaveStaffDetails(RegisterStaffViewModel model,HttpPostedFileBase image) 
{ 
    if (Request.Files.Count > 0) { 
     string FileName = Guid.NewGuid().ToString().Replace("-", ""); 
     string Path = System.IO.Path.GetExtension(Request.Files[0].FileName); 
     string FullPath = "~/Images/StaffPhotos/" + FileName + Path; 
     Request.Files[0].SaveAs(Server.MapPath(FullPath)); 
     staffPhoto = FileName + Path; 
    } 
} 

당신은 당신이 VAR 직원 = 새로운 staffTable() 위의 컨트롤러에 표시된 것을 추가

<input type="file" class="filestyle" name="image" data-classbutton="btn btn-primary" data-input="false"> 
+0

파일 후 입력이 필요합니다. 그것은하지 않습니다. staffPhoto는 현재 컨텍스트에 존재하지 않습니다. – slider1578

+0

photopath를 포함 할 모델에는 변수가 없으므로 저장하지 않습니다. 포함되는 문자열 public string somethingPath {get; } db에서 photopaths에 속하는 열이 있어야합니다. – Trinity

관련 문제