문제

MVC 5 와이 같은 면도기 5 페이지 코드가 있습니다.

<input type="file" name="file" />

<img src=@Url.Content(Model.ImagePath)" alt="Image"/>
.

새 항목을 만들고 입력 유형 파일로 이미지를 추가하면 작동합니다.그러나 편집합니다. edit.cshtml에서 동일한 코드를 가지고 있으며 모델 및 ID에서 입력 유형 파일을 설정하고 싶지 않습니다.이 입력 파일을 설정할 수 없습니다.

감사합니다

도움이 되었습니까?

해결책

모델에서 속성을 만들어야합니다.

public class ViewModel
{
    public string ImagePath{ get; set; }
.....


}
.

편집보기 :

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new 
                                       { enctype = "multipart/form-data" }))
{
    <label for="ImageUpload">Filename:</label>
    <input type="file" name="ImageUpload" id="ImageUpload" />
}
.

컨트롤러 :

[HttpPost]
public ActionResult Action(ViewModel model)
{
    if (ModelState.IsValid)
    {

        var file = Request.Files["ImageUpload"];
        if (file != null && file.ContentLength > 0){
            var uploadDir = "~/uploads"
            var imagePath = Path.Combine(Server.MapPath(uploadDir), file.FileName);
            var imageUrl = Path.Combine(uploadDir, file.FileName);
            file.SaveAs(imagePath);
            model.ImagePath= imageUrl;
        }

    }
}
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top