使用视图页面中的部分视图

发布于 2024-10-26 15:13:23 字数 3781 浏览 1 评论 0原文

我正在使用普通视图页面。我有一个 AJAX 表单。当我单击此表单上的提交按钮时,我必须能够更新部分视图。然而,我尝试过这个选项,发现页面请求需要很多时间来渲染,我确信我把这个过程搞砸了更多。

这是我使用的代码,

  1. 在控制器内部,

    公共 ActionResult Index()
    {
        用户 allUsers = new Users();
        if (Request.IsAjaxRequest())
        返回视图(“索引”,allUsers.GetAllUsers());
       别的
        返回视图(allUsers.GetAllUsers());
    }
    

部分视图[index.ascx]

    <%@ Control Language="C#"    Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Model.Users>>" %>
   <table>
     <tr>
      <th>
     </th>
        <th>
             firstname
       </th>
       <th>
             userid
       </th>
       <th>
             dob
       </th>
       <th>
          address
       </th>        
  </tr>
  <% foreach (var item in Model)
      { %>
   <tr>
      <td>
         <%: Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %>
         |
         <%: Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%>
         |
         <%: Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%>
     </td>
     <td>
         <%: item.firstname %>
     </td>  
     <td>
         <%: item.userid %>
     </td>
     <td>
        <%: String.Format("{0:g}", item.dob) %>
    </td>
    <td>
        <%: item.address %>
    </td>
   </tr>
          <% } %>
    </table>
   <p>
    <%: Html.ActionLink("Create New", "Create") %>
     </p>

index.aspx页面,

        <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <% using (Ajax.BeginForm("Index", new AjaxOptions
       {
           UpdateTargetId = "results"
       }))
           { %>
        <input id="Submit1" type="submit" value="submit" />
        <% } %>
        <div id="results">
            <% Html.RenderPartial("Index", ViewData.Model); %>
        </div>
        <h2>
            Index</h2>
        <table>
            <tr>
                <th>
                </th>
                <th>
                    firstname
                </th>
                <th>
                    userid
                </th>
                <th>
                    dob
                </th>
                <th>
                    address
                </th>
            </tr>
            <% foreach (var item in Model)
               { %>
            <tr>
                <td>
                    <%: Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %>
                    |
                    <%: Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%>
                    |
                    <%: Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%>
                </td>
                <td>
                    <%: item.firstname %>
                </td>
                <td>
                    <%: item.userid %>
                </td>
                <td>
                    <%: String.Format("{0:g}", item.dob) %>
                </td>
                <td>
                    <%: item.address %>
                </td>
            </tr>
            <% } %>
        </table>
        <p>
            <%: Html.ActionLink("Create New", "Create") %>
        </p>
    </asp:Content>

I am using the normal view page. I have an AJAX form. When I click on the submit button on this form, i must be able to get the partial view updated. However I have tried with this option and found that the page request is needing a lot of time to render and I am sure that I messed up with this process more.

Here is the code that I have used,

  1. Inside the controller,

    public ActionResult Index()
    {
        Users allUsers = new Users();
        if (Request.IsAjaxRequest())
        return View("Index", allUsers.GetAllUsers());
       else
        return View(allUsers.GetAllUsers());
    }
    

partial view [index.ascx]

    <%@ Control Language="C#"    Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Model.Users>>" %>
   <table>
     <tr>
      <th>
     </th>
        <th>
             firstname
       </th>
       <th>
             userid
       </th>
       <th>
             dob
       </th>
       <th>
          address
       </th>        
  </tr>
  <% foreach (var item in Model)
      { %>
   <tr>
      <td>
         <%: Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %>
         |
         <%: Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%>
         |
         <%: Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%>
     </td>
     <td>
         <%: item.firstname %>
     </td>  
     <td>
         <%: item.userid %>
     </td>
     <td>
        <%: String.Format("{0:g}", item.dob) %>
    </td>
    <td>
        <%: item.address %>
    </td>
   </tr>
          <% } %>
    </table>
   <p>
    <%: Html.ActionLink("Create New", "Create") %>
     </p>

index.aspx page,

        <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <% using (Ajax.BeginForm("Index", new AjaxOptions
       {
           UpdateTargetId = "results"
       }))
           { %>
        <input id="Submit1" type="submit" value="submit" />
        <% } %>
        <div id="results">
            <% Html.RenderPartial("Index", ViewData.Model); %>
        </div>
        <h2>
            Index</h2>
        <table>
            <tr>
                <th>
                </th>
                <th>
                    firstname
                </th>
                <th>
                    userid
                </th>
                <th>
                    dob
                </th>
                <th>
                    address
                </th>
            </tr>
            <% foreach (var item in Model)
               { %>
            <tr>
                <td>
                    <%: Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %>
                    |
                    <%: Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%>
                    |
                    <%: Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%>
                </td>
                <td>
                    <%: item.firstname %>
                </td>
                <td>
                    <%: item.userid %>
                </td>
                <td>
                    <%: String.Format("{0:g}", item.dob) %>
                </td>
                <td>
                    <%: item.address %>
                </td>
            </tr>
            <% } %>
        </table>
        <p>
            <%: Html.ActionLink("Create New", "Create") %>
        </p>
    </asp:Content>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲜血染红嫁衣 2024-11-02 15:13:23

请尝试

return PartialView("index",allUsers.GetAllUsers())

如果有ajax请求, 。我怀疑当你调用时控制器正在发送视图页面,

return View("index", allUsers.GetAllUsers());

这又会渲染部分页面,从而在过程中造成某种混乱。相反,您可以尝试将部分页面重命名为index1并

return View("index1", allUsers.GetAllUsers());

Please try

return PartialView("index",allUsers.GetAllUsers())

in case of ajax request. i suspect that controller is sending the Viewpage when u call

return View("index", allUsers.GetAllUsers());

which in turns render the partial page hence creating some kind of mess during the process. conversely, u can try renaming your partial page to index1 and

return View("index1", allUsers.GetAllUsers());
多孤肩上扛 2024-11-02 15:13:23

我自己解决了这个问题,这是一个很好的挑战。我已将 ajax 链接放入用户控件中,然后将结果 div 放入索引页中,其中使用相同的分部视图控件呈现数据。

然后在控制器的操作方法中,我调用了返回部分视图(视图名称和模型数据)。

这就是窍门..把事情完成了。无论如何,感谢您迄今为止的帮助。

It was a nice challenge that I had solved the problem by myself. I had put the ajax links inside the usercontrol and then put the results div in the index page inside which the data was rendered using the same partialview control.

Then in the controller's action method, i called the return partialview(viewname and model data).

That's the trick.. got things completed. Anyways thanks for your assistance so far.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文