Display partialview when clicking a link

2020-05-07 05:09发布

I'm fairly new to JQuery and MVC 2 which makes this a bit harder, I've always used WebForms before.
I'm using the NerdDinner example as setup (I've switched Dinner for Person though).
What I want to do is pretty simple. In a table I want a small modal display with details when a "details" link is clicked in the table.
Right now when I click a link I open "Details.ascx" in a new window, it's not adding it to the details div

Search page (View)

Link in table: (foreach (var item in Model))

<%= Ajax.ActionLink("Details", "Details", new { id = item.id}, new AjaxOptions() { UpdateTargetId = "details" })%>  

javascript:

<script type="text/javascript">
    $('tr').click(function() {
        $("#details").load("Persons/Details/1")
    });
</script>

div to contain details:

<div id="details" class="detailView">details here</div>

Partialview:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Person>" %>
<%@ Import namespace="RegistryTest.Models" %>
<div id="detailsModal">
    <%= Html.Encode(Model.surname) %>
</div>

Controllers

public PartialViewResult Details(int id)
    {
        Person person = repository.GetPerson(id);

        return PartialView("Details", person);
    }

Let me know if you need more info on the setup.

2条回答
狗以群分
2楼-- · 2020-05-07 05:32

page1.asp

<div class="holder">
    <div>1</div>
    <div class="test">2</div>
</div>
$('.holder').load('page1.asp .test');// returns =>> 2 // .test grabs any element that has a .test and pushes it into the holder

so page would look like this...

<div class="holder">
    <div class="test">2</div>
</div>
查看更多
家丑人穷心不美
3楼-- · 2020-05-07 05:34

Ignore the Ajax helpers, they're for ASP.NET AJAX scripting.

Change your link to:

<%= Html.ActionLink("Details", "Details", new { id = item.id})%> 

And your jQuery to:

    $('tr').click(function() {
        var url = jQuery(this).find('a').attr('href');
        $("#details").load(url)
    });
查看更多
登录 后发表回答