MVC+ExtJs 인증 정보의 원활한 결합
3127 단어 ExtJs
/// <summary>
/// Ext Ajax
/// </summary>
/// <param name="success"> </param>
/// <param name="value"> </param>
/// <returns></returns>
protected ActionResult ExtJson(bool success)
{
return ExtJson(success, 0, string.Empty);
}
/// <summary>
/// Ext Ajax
/// </summary>
/// <param name="success"> </param>
/// <param name="value"> </param>
/// <returns></returns>
protected ActionResult ExtJson(bool success, object value)
{
return ExtJson(success, 0, value);
}
/// <summary>
/// Ext Ajax
/// </summary>
/// <param name="success"> </param>
/// <param name="value"> </param>
/// <returns></returns>
protected ActionResult ExtJson(bool success, int code, object value)
{
return Json(new { success = success, code = code, value = value }, "text/html", System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet);
}
/// <summary>
/// Model
/// </summary>
/// <returns></returns>
protected ActionResult ModelError()
{
List<object> errors = new List<object>();
foreach (var key in ModelState.Keys)
{
var state = ModelState[key];
foreach (var error in state.Errors)
{
errors.Add(new { id = key, msg = error.ErrorMessage });
}
}
if (errors.Count > 0)
{
return ExtJson(false, errors);
}
return ExtJson(false, " , !");
}
action 호출 예:
if (!ModelState.IsValid)
{
return ModelError();
}
2. 클라이언트 코드
handler: function() {
var form = theForm.getForm();
if (form.isValid()) {
form.submit({
waitMsg: ' ...',
success: function(form, action) {
Ext.Msg.alert(' ', ' !', function() {
win.hide();
store.loadPage();
});
},
failure: function(form, action) {
if (action.result) {
if (typeof(action.result.value) == 'string') Ext.Msg.alert(' ', action.result.value);
else if (action.result.value.constructor == Array) theForm.getForm().markInvalid(action.result.value);
} else {
Ext.Msg.alert(' ', ' ');
}
}
});
}
}