2012-03-29 83 views
3

這是我第一次使用部分視圖,我無法得到實際的partialview。 ajax函數被調用,控制器被擊中,並且ajax調用中的一個警報告訴我partialview在那裏。但是,有錯誤寫入控制檯(或警報)我的div仍然是空的。 我的應用程序是一個MVC4應用程序,但我很確定我剛剛在某處做了一個愚蠢的錯誤,而不是MVC的錯誤:)部分視圖不渲染MVC剃鬚刀

幾個小時後,我真的很高興,任何人都可以幫助我這工作,和代碼/阿賈克斯的所有提示/意見,我非常感謝!關於資料/指數

public PartialViewResult Groups() 
{ 
     var person = _userRepository.GetCurrentUser(); 
     var connections = (from c in person.Person1 select c).ToList(); 
     var groups = _context.Groups.Where(g => g.GroupId == 1); 

     var all = new GroupViewModel() 
         { 
          Connections = connections, 
          GroupDetailses = (from g in groups 
              select 
               new GroupDetails 
                { 
                 Name = g.Name, 
                 StartDate = g.StartDate, 
                 StartedById = g.StartedById, 
                }) 

         }; 
     return PartialView("Groups",all); 
} 

我PartialView

@model Mvc4m.Models.GroupViewModel 

<h2>Groups</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<h3>Create new Group</h3> 
<div class="ui-widget"> 
    <label for="group">Groupname: </label> 
    <input id="group" /> 
    <button onclick="addGroup()">Add</button> 
</div> 

@foreach (var item in Model.GroupDetailses) 
{ 
    @Html.LabelFor(model => item.Name)<text> : </text> 
    @Html.DisplayFor(model => item.Name) 
} 
<script> 
    function addGroup() { 
     $.get(
       "/Profile/AddGroup", 
       { 
        Name: $("#group").val() 
       }); 
     location.reload(); 
    }; 
</script> 

我的Ajax調用

@model Mvc4m.Models.ProfileView 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<div id="test" style="background-color:Aqua; width:200px; height:100px"></div> 

<button onclick="load()"></button> 

<script type="text/javascript"> 
function load() { 
    $.ajax({ 
     type: "GET", 
     url: '/Profile/Groups', 
     dataType: 'html', 
     success: function (response) { 
      $('#test').html(response); 
      alert(response); 
     }, 
     error: function (xhr, status, error) { 
      alert(status + " : " + error); 
     } 

    }); 
} 

</script> 
+0

嚴重的是,我無法獲得這個網站上的格式化操作:(我打算通過四個空格代碼部分,怎麼會這樣呢? – 2012-03-29 23:08:32

+0

控制檯中的錯誤是什麼? – Shyju 2012-03-29 23:28:33

回答

0

原來的代碼工作,Visual Studio中的重新啓動做的伎倆!我有時候很討厭VS ......

+0

你使用的是什麼版本的Visual Studio? – 2012-03-30 00:44:38

+0

2010終極版,它已經和我每天幾次「分手」了:/ – 2012-03-30 19:26:06

+0

呃,這沒什麼好玩的,我一直在使用Beta版的Visual Studio 11,它看起來很穩定,所以你可以試試看,順便問一下,你的網站有什麼問題(perfectdiet.com)?我只是得到一個「Forbidden」的錯誤 – 2012-03-30 20:02:11