У меня есть класс модели:
public class Register
{
public Employee Employee { get; set; }
public Business Business { get; set; }
}
У меня есть HTML-форма с текстом типа ввода с данными Employee и Business из модели и файлом типа ввода для загрузки изображения:
@using (Html.BeginForm(null, null, FormMethod.Post, new { @id = "frmRegister", @enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="div-file">
<input id="inputFile" title="Upload a business image" type="file" name="UploadedFile" accept="image/*" />
</div>
<div class="div-input">
@Html.Label("Name:", htmlAttributes: new { @for = "txtName" })
@Html.EditorFor(model => model.Employee.Name, new { htmlAttributes = new { @class = "form-control", @id = "txtName" } })
</div>
<div class="div-input">
@Html.Label("Age:", htmlAttributes: new { @for = "txtAge" })
@Html.EditorFor(model => model.Employee.Age, new { htmlAttributes = new { @class = "form-control", @id = "txtAge" } })
</div>
<div class="div-input">
@Html.Label("Company:", htmlAttributes: new { @for = "txtCompany" })
@Html.EditorFor(model => model.Business.Name, new { htmlAttributes = new { @class = "form-control", @id = "txtName" } })
</div>
<div class="div-input">
@Html.Label("Phone:", htmlAttributes: new { @for = "txtPhone" })
@Html.EditorFor(model => model.Business.Phone, new { htmlAttributes = new { @class = "form-control", @id = "txtPhone" } })
</div>
<div class="text-center">
<input type="button" id="btnRegister" value="Register" class="btn btn-default" />
</div>
}
Я беру информацию со входов с помощью JQuery и передаю на контроллер с AJAX:
@section Scripts {
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$("#btnRegister").on('click', function (e) {
e.preventDefault();
var image = document.getElementById("inputFile").files[0];
var frmRegister = $("#frmRegister").serialize();
$.ajax({
url: '@Url.Action("Create", "Register")',
type: 'POST',
traditional: true,
data: frmRegister,
dataType: 'json',
ContentType: "application/json;utf-8",
cache: false,
success: function (response) {
},
error: function (response) {
alert(response.responseText);
}
});
});
});
</script>
}
Контроллер:
public ActionResult Create(FormCollection collection)
{
//HttpPostedFileBase file = Request.Files["UploadedFile"];
return View();
}
Вопрос: Как передать файл изображения тоже