선택한 파일의 전체 경로를보기의 모델로 설정하려고합니다.보기에서 모델링 할 선택된 파일의 경로를 설정하십시오.
컨트롤러 FileController :
public async Task<IActionResult> Create(CreateFileViewModel model)
{
if (ModelState.IsValid)
{
var file = new File
{
Path = model.Path
};
_context.Add(file);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(model);
}
모델 CreateFileViewModel :
public class CreateFileViewModel
{
public string Path { get; set; }
}
모델 파일 :
public class File
{
public int Id { get; set; }
public string Path { get; set; }
}
ViewForm 만들기 :
<form asp-action="Create">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="Path" class="control-label"></label>
<input asp-for="Path" id="selectedFile" type="file" />
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
스크립트의 생성이 작동하지
<script>
document.getElementById('selectedFile').onmouseout = function() {
@Model.Path=this.value;
};
</script>
그러나
@Model.Path=this.value;
. Ofc 나는 면도칼과 자바 스크립트 변수 사이를 변환 할 수 없습니다. 그러나 나는 모델 변수에 선택된 파일의 전체 경로를 설정하는 또 다른 방법을 모른다. 모델 변수로 이
<input asp-for="Path" id="selectedFile" type="file" />
세트는 경로없이 파일 이름.