-1
我正在創建註冊頁面。我希望用戶在註冊時上傳圖片。我用兩個參數在控制器上創建了一個動作。一個是模型,另一個是HttpPostFileBase對象。但我不能同時通過這兩個對象。我嘗試了很多方法,但我無法弄清楚。謝謝。將Json對象和FormData(文件)發佈到控制器,參數爲MVC
點擊寄存器調用Register()metod。
function Register() {
var team = { id: $("#team").val() }
var account =
{
eMailAddress: $("#eMailAddress").val(),
userName: $("#userName").val(),
password: $("#password").val(),
password2: $("#password2").val(),
firstName: $("#firstName").val(),
lastName: $("#lastName").val(),
gender: $("#gender").val(),
team: team,
birthday: $("#birthday").val(),
phoneAreaCode: $("#phoneAreaCode").val(),
phoneLocalNumber: $("#phoneLocalNumber").val()
}
var imageFile = $('#image').get(0).files[0];
var data = new FormData();
data.append("image", imageFile);
data.append('account', account);
console.log(typeof data);
$.ajax
({
url: "/Registration/Register",
type: "POST",
data: data,
contentType: false,
processData: false,
success: function (response) {
$("#message").hide();
$("#message").empty();
if (response.isSucceeded) {
window.location.replace("/Login/Login");
}
else {
$.each(response.errors, function (i, error) {
$("#message").append(error + "<br>");
});
$("#message").fadeIn();
}
}
});
}
和有控制器動作
[HttpPost]
public ActionResult Register(HttpPostedFileBase image,Account account)
{
account.team = serviceFactory.Run<GetTeamByIdResponse>(x => x.GetTeamById(new GetTeamByIdRequest(account.team.id))).team;
account.GenerateVerificationCode();
CreateAccountRequest request = new CreateAccountRequest();
request.account = account;
request.image = image;
request.server = Server;
CreateAccountResponse response = serviceFactory.Run<CreateAccountResponse>(x => x.CreateAccount(request));
if (response.isSucceeded)
{
//toDo
}
return Json(response);
}
<div class="kayit-ol-box">
<ul>
<li>
E-posta<br>
<input id="eMailAddress" type="text">
</li>
<li>
Kullanıcı Adı<br>
<input id="userName" type="text">
</li>
<li>
Şifre<br>
<input id="password" type="password">
</li>
<li>
Şifre Tekrar<br>
<input id="password2" type="password">
</li>
<li>
Ad<br>
<input id="firstName" type="text">
</li>
<li>
Soyad<br>
<input id="lastName" type="text">
</li>
<li>
Cinsiyet<br>
<select id="gender">
<option value="Erkek">
Erkek
</option>
<option value="Kadın">
Kadın
</option>
</select>
</li>
<li>
Doğum Tarihi<br>
<input id="birthday" type="date" value="" />
</li>
<li>
Cep Telefonu<br>
<input class="alankodu" type="number" id="phoneAreaCode" />
<input class="telefon" id="phoneLocalNumber" type="number" step="1" min="10">
</li>
<li>
Tuttuğunuz Takım<br>
<select id="team">
<option selected value="0">
Tutmuyorum
</option>
@if (Model != null)
{
foreach (Team team in Model)
{
<option value="@team.id">
@team.name
</option>
}
}
</select>
</li>
<li>
Avatar Ekle<br>
<span>Maximum 700 kb (jpg, png, gif)</span><br>
<input class="" id="image" name="image" type="file">
</li>
<li>
<input type="checkbox" id="izin" value="1" class="css-checkbox" checked="checked" />
<label for="checkbox21" id="izin" class="css-label">
Üyelik Sözleşmesi 'ni okudum ve kabul
ediyorum.
</label>
<br>
<br>
<button class="kayit-submit" type="button" id="submit" onclick="Register()">ÜYELİĞİ TAMAMLA</button>
</li>
我不能同時通過它。其中一個始終爲空
什麼是你的服務器代碼的語言? – Neal
@最新問題已更新。 –
不,你沒有... – Neal