2012-03-24 39 views
2

嗨有一個頁面加載一個jQuery模式對話框,該模式包含一個表單。我哪有那麼它加載返回一個錯誤的模式,如果我返回PartialView,頁面只是用對話形式,內容加載(這只是意在模態)返回驗證錯誤回到MVC中的jQuery對話框3

這裏是視圖

<span><a id="end" href="#">Launch End Dialog</a> </span> 



<div id="end-dialog" class="dialog"> 
    <div id="end-inner"></div> 
    <hr /> 
</div> 

<script type="text/javascript"> 
    $(function() { 


    var endDialog = $("#end-care-plan-dialog").dialog({ 

    }); 

    $("#end").click(function() { 
     endDialog.dialog('close'); 
    }); 

    $('#end').click(function() { 
     $('#end-inner').load(baseUrl + "/End", $.param({ id: '@Model.id' }), 
       function() { 
       endDialog.dialog('open'); 
       }); 
    }); 
    }); 
</script> 

我的繼承人控制器

[HttpPost] 
public ActionResult End(EndVM end) 
{ 
    if (ModelState.IsValid) 
    { 
    //do work 

    } 

    //return so that pop up now has validation errors 
    return PartialView(end); 
} 

注 - jquery.validate和jquery.validate.unobstrusive裝入意見

感謝希望這是有道理的。

更新:迭 - 這是包含表單(即加載到moal)的局部視圖

@using (Html.BeginForm<EndController>(c => c.End(@Model.Id))) 

{ @ Html.HiddenFor(M => m.Id) @Html .DropDownListFor(M => m.EndReasonId,RefData.EndReasons) 其它理由 @ Html.EditorFor(M => m.EndReasonOther) @ Html.ValidationMessageFor(M => m.EndReasonOther) @( Html.ActionLi NK(C => c.Edit(@ Model.Id), 「取消」))@ Html.SubmitButton( 「結束」, 「結束」) }

是否有一個地方我適應您的代碼?

謝謝

+0

您可以使用ajax使它更簡單 – 2012-03-24 22:11:46

回答

1

它確實有道理。你只需確保表單通過AJAX提交,否則整個頁面將刷新。要覆蓋默認行爲,您需要處理表單的submit事件。假設您的表單是myForm

$('#myForm').submit(function() { // catch the form's submit event 
    $.ajax({ 
     data: $(this).serialize(), // get the form data 
     type: $(this).attr('method'), // GET or POST 
     url: $(this).attr('action'), // the file to call 
     success: function(response) { // on success.. 
      $('#end-inner').html(response); // update the DIV 
     } 
}); 
return false; // cancel original event to prevent form submitting 
});