2017-02-18 14 views
0

我使用asp.net mvc5。我在網站(佈局)的右側面板中創建一個菜單。當用戶點擊菜單項時,無需刷新頁面即可加載頁面。爲此,我使用Ajax.ActionLink如何重定向到使用ajax查看?

<aside class="right-panel flexcol"> 
<button class="accordion">Hotels</button> 
    <div class="sub-panel flexcol"> 
@Ajax.ActionLink("View Hotels", "Index", "Hotel", null, new AjaxOptions { HttpMethod = "get", UpdateTargetId = "pageContent" }, new { @class = "sub-menu" }) 
@Ajax.ActionLink("HotelType", "Index", "HotelType", null, new AjaxOptions { HttpMethod = "get", UpdateTargetId = "pageContent" }, new { @class = "sub-menu" }) 

    </div> 

和呼叫'新酒店頁面,我使用

@Ajax.ActionLink("Create Hotel", "Create", "Hotel", null, new AjaxOptions { HttpMethod = "get", UpdateTargetId = "pageContent" }, new { @class = "bluelink" }) 

這些工作正常。 問題:當我提交創建頁面,此頁面發送給控制器,如果模型填充無效的數據,頁面重定向到創建頁面沒有佈局和css的。

@model ... Models.Hotel 
@{ 
ViewBag.Title = "Create"; 
Layout =null; 
} 

控制器

public ActionResult Create() 
    { 
     return PartialView(); 
    } 


    [HttpPost] 
    public ActionResult Create(Hotel model) 
    { 
     if (!ModelState.IsValid) 
     { 
      return PartialView(model); 
     } 
     ApplyToDataBase(model, "uspInsertHotel"); 
     return RedirectToAction("Index"); 
    } 

enter image description here

回答

1

在我看來,這個問題是事實,你是無效的模型的情況下返回一個局部視圖,這是不是一個Ajax調用。我可以建議幾個選項,我希望他們能提供幫助。

首先,您可以不返回部分視圖,而是返回該情況下的整個視圖。這可能不是非常優雅的解決方案,它可能會導致視圖重複。

另一種方式 - 使用客戶端驗證,所以你不必做一個服務器調用來驗證你的輸入。在這種情況下,您可能需要使用jQuery驗證插件。