私はajax.beginformを使用して、別のビュー内の部分ビューを作成しています。Ajax.beginForms MVCパーシャルビュー
私はユーザーが正しいsnを入力しても問題ありません。 しかし、ユーザーが無効な番号を入力した場合は、インデックスビューにリダイレクトします。
これで、インデックスページが部分ビューとして送信されました。
どうすればそれを避けることができますか?
ここに私のビューの一部と2つの簡略化されたアクションの結果があります。
@using (Ajax.BeginForm("MachineInfo", "QrCreate", new AjaxOptions() {
HttpMethod = "POST", UpdateTargetId = "form-content", InsertionMode =
InsertionMode.ReplaceWith }))
{
@Html.AntiForgeryToken()
<input type="text" id="sn" name="sn" class="inputsn"
placeholder="Enter your serial number here..." />
<input type="submit" value="Search" class="search btn btn-success btn-lg" />
}
</div>
</div>
<div id="form-content"></div>
のAjax呼び出しがリダイレクトされません
public ActionResult Index(bool? isValidMachine = null)
{
ViewBag.invalidSerialNumber = isValidMachine;
return View();
}
[HttpPost]
public ActionResult MachineInfo(string sn)
{
if(string.IsNullOrEmpty(sn))
RedirectToAction("Index", new { isValidMachine = false });
QrCreateViewModel qrCreateVM;
using (var machineService = new MachineApiService())
{
var machine = machineService.GetMachineFromSerialNumber(sn);
if (machine == null)
return RedirectToAction("Index", new { isValidMachine = false });
else
qrCreateVM = new QrCreateViewModel(machine, GetBasePath());
}
if (qrCreateVM.IsValid())
{
qrCreateVM.Viewurl = qrCreateVM.QrCreateUrlOrDefaultNull();
return PartialView(qrCreateVM);
}
else
return RedirectToAction("Index", new { isValidMachine = false });
}
のAjax呼び出しをリダイレクトすることはできませんリダイレクトするために、次のスクリプトを追加で次に
- それらの全体のポイントは、滞在することです同じぺージに。 1つのオプションは、部分的なビューを返さない条件でHttpエラーをスローし、 'OnFailure' ajaxオプションを使用して' Index() 'メソッドにリダイレクトされるスクリプトを呼び出すことです。あなたは古くなった 'Ajax.BeginForm()'を取り除き、さらに柔軟性を与える '$ .ajax()'を使います。 –
あなたの答えをありがとう私はあなたの最初の提案を理解しようとしました。 beterオプションはJquery Ajaxで動作することですが、 の決定は私の手元にありません。 あなたのコメントを良い答えとして受け入れたいと思いますが、それはうまくいかず、 –
その後ろに追加します朝の詳細な答え –