简体   繁体   English

使用Ajax,MVC 3返回部分视图

[英]Returning a partial view with Ajax, MVC 3

First let me say I think this site is a Godsend, and I am extremely grateful for all I've learned. 首先,我想说我认为这个网站是天赐之物,对于我所学到的一切,我深表感谢。 I have an MVC3 component I'm working with. 我有一个正在使用的MVC3组件。 I want to populate a selectlist and when the user selects one of the options, I want a to load a partial view with the data displayed. 我想填充一个选择列表,当用户选择其中一个选项时,我想使用显示的数据加载局部视图。 Everything works so far except I'm missing the piece that refreshes the partial view. 到目前为止,一切工作正常,但我错过了刷新局部视图的部分。 When the page originally loads, I see the empty container in the code. 最初加载页面时,我在代码中看到了空容器。 I get no JS errors and Firebug shows the properly formatted HTML returned. 我没有收到JS错误,Firebug显示返回的格式正确的HTML。 So what part of the operation am I missing to refresh the partial view on the page? 那么,我缺少该操作的哪一部分来刷新页面上的部分视图? Please advise and thanx in advance. 请提前告知和谢谢。

The View: 风景:

    <tr>
  <th>Select a User to view their Roles:</th>
  <td><%=  Html.DropDownList("UserListForRoleView", list, "Please choose a User")%></td>
</tr>
<tr>
   <td>
      <% Html.RenderPartial("ViewUsersInRole");%>
    </td>
 </tr>

$(document).ready(function () {
  $('#UserListForRoleView').change(function () {
     var selectedID = $(this).val();
     $.get('/UserAdminRepository/ViewUsersInRole/?user=' + selectedID)
    });
 }); 

THe Controller: 控制器:

   public ActionResult ViewUsersInRole()
    {
        string user = Request["user"];
        string[] selectedRoles = Roles.GetRolesForUser(user);
        List<string> data = new List<string>(); 
        if (selectedRoles.Length > 0)
        {
            data = selectedRoles.ToList<string>();
        }
        else
        {
            data.Add("No data found");
        }

        ViewData["UsersinRole"] = data;

        return PartialView("ViewUsersInRole");
    }

The PartialView (in it's entirety): PartialView(全部):

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
  <ul>
<%    
   List<string> list = ViewData["UsersinRole"] as List<string>;
   if (list != null && list.Count > 0)
   {
        foreach (string item in list)
      { %>
        <li><%: item %></li>
     <% }
   }
%>
</ul> 

在此处输入图片说明

append html in the td like this: 像这样在td附加html:

<td id="partialcontainer">
  <% Html.RenderPartial("ViewUsersInRole");%>
</td>

and append html in it: 并在其中附加html:

$(document).ready(function () {
   $('#UserListForRoleView').change(function () {
       var selectedID = $(this).val(); 
    $.get('/UserAdminRepository/ViewUsersInRole/?user=' +  selectedID,function(response){

     $('#partialcontainer').html(response); 
          })
         });                            
       });

Little Improved code, always use Url.Action to generate url: 改进很少的代码,请始终使用Url.Action生成url:

$(document).ready(function () {

$('#UserListForRoleView').change(function () {

var selectedID = $(this).val();

var url = '@Url.Action("ViewUsersInRole","UserAdminRepository")';

url+"?user="+selectedID;

$.get(url,function(response){

$('#partialcontainer').html(response);

 });

 });

 });

I have modified a little your code to make it more clear and readable. 我对您的代码做了一些修改,以使其更清晰易读。

尝试RenderAction

<% Html.RenderAction("ViewUsersInRole");%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM