0

javascript でフォームを送信した後、モデル状態エラーを同じモーダル ダイアログにロードすることは可能ですか?

私のコードは次のようなものです:

コントローラ:

public ActionResult Create(MyModel model){
     if(ModelState.isValid){
           // DB Save
           return RedirectToAction("Index");
     }
     else{
           return View(model);
     }
}

Ajax メソッド

$.ajax({
       type: 'POST',
       url: '/Receipt/Create',
       cache: false,
       data: $("#CreateForm").serialize(),
       success: function (e) { window.location="/Controller/Action"; },
       error: function (e) { e.preventDefault(); /*Code here to load model error into page*/ }                
});
4

2 に答える 2

1

私は今日、この問題をこのようなもので解決しました

public ActionResult Create(MyModel model){
 if(ModelState.isValid){
       // DB Save
       return RedirectToAction("Index");
 }
 else{
       return PartialView("_myPartialForm",model);
 }
}

$.ajax({
   type: 'POST',
   url: '/Receipt/Create',
   cache: false,
   data: $("#CreateForm").serialize(),
   success: function (e) { 
if(e.Valid){
    window.location="/Controller/Action";}
else{
    return false;
  } },
   error: function (e) { e.preventDefault();$("#mymodal").load(e) } 
 });

jmrnetが言ったようなものです。ありがとう

于 2013-01-16T15:45:54.997 に答える
0

Ajax.BeginFormメソッドをUpdateTargetIdAjaxOptionと共に使用することで、これを実現できました。これが私が使用したコードです。それはあなたがしていることに正確には適合しませんが、正しい方向にあなたを向けるはずです.

ビューで:

@using (Ajax.BeginForm(new AjaxOptions(){ UpdateTargetId="loginresult" }))
{
    <b>User:</b><br />
    @Html.TextBoxFor(m => m.UserName)<br />
    <br />
    <b>Password:</b><br />
    @Html.PasswordFor(m => m.Password)<br />
    <div id="loginresult"><br /></div>
    <input id="Button1" type="submit" value="Login" class="touch-button" />
}

コントローラーで:

[HttpPost]
public ActionResult Index(LoginModel model)
{
    //Execute Log-in code.
    //Capture any errors and put them in the model.LoginResponse property.

    return PartialView("LoginResult", model);
}

部分LoginResultビュー:

@model MerchantMobile.Models.LoginModel

@if (String.IsNullOrEmpty(Model.LoginResponse))
{
    Html.RenderPartial("_AjaxRedirect", Url.Content("~/Home/Activity"));
}
else
{
    <div id="loginresult">
        <div style="color: Red; font-weight: bold;">
            @Model.LoginResponse
        </div>
    </div>
}

loginresult <div>div にテキストを表示するだけでなく、モーダル ダイアログ ボックスをポップアップするために jquery ui で使用されるものに簡単に置き換えることができます。

お役に立てれば!

于 2013-01-16T15:28:59.617 に答える