2016-04-28 176 views
1

在我的EspaceClient/Compte.cshtml我回顧了部分視圖。 它可以工作,但是當ModelState無效時,我只檢索沒有主視圖的局部視圖。模型無效返回部分視圖沒有主視圖

謝謝

function partialCoordonnees() { 
     $.ajax({ 
     url: '/Utilisateurs/Edit/'+ @Model.Id, 
     dataType: "html", 
     success: function (data) { 
     $('.fifth').html(data); 
     } 
     }); 
}; 

在我UtilisateursController

public ActionResult Edit(int? id) 
{ 
    Utilisateur utilisateur = db.Utilisateurs.Find(id); 
    return PartialView("_CoordonneesCompte", utilisateur); 
} 


public ActionResult Edit(Utilisateur utilisateur) 
{ 
    if (ModelState.IsValid) 
    { 
     db.Entry(utilisateur).State = EntityState.Modified; 
     db.SaveChanges(); 
     return RedirectToAction("Index", "EspaceClient", new { id=utilisateur.Id }); 
    } 
     return PartialView("_CoordonneesCompte", utilisateur); 
} 
+0

檢查呈現的HTML - 它可能是整個頁面插入目標元素。 AJAX不會遵循重定向請求。如果您想要重定向,您需要檢查302重定向的響應,並在回調中使用JavaScript導航。 – Jasen

+0

第一次調用局部視圖是在整個頁面中$('。fifth')。html(data); 問題是在模型無效時返回PartialView(「_ CoordonneesCompte」,utilisateur)。 我不知道我怎麼做你推託我的東西 – Mercenaire

回答

1

ModelState.IsValid

執行服務器端驗證一起去unobstrusive jQuery庫

客戶端驗證

添加jquery.unobtrusive-ajax.min.jsjquery.validate.min.js在CSHTML

[實施例]

//this code goes in your partialview 
$(function(){ 
    //allow the validation framework to re-prase the DOM 
    jQuery.validator.unobtrusive.parse(); 

    //or to give the parser some context, supply it with a selector 
    //jQuery validator will parse all child elements (deep) starting 
    //from the selector element supplied 
    jQuery.validator.unobtrusive.parse("#formId"); 
}); 


//then in your parent page handle the form submission 
$(function(){ 
    $("#SubmitButton").click(function(){ 
    if (!$("#Form1").valid()){ 
     return false; 
    } 
    }); 
}); 

該示例中採取形式Here

希望它可以幫助你...

相關問題