Ajax 请求获取部分视图

发布于 2024-10-18 14:04:11 字数 2019 浏览 0 评论 0原文

我的主视图如下:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

指数

<script src="../../Scripts/jquery-1.3.2.js" type="text/javascript"></script>

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>

<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>

<h2>
    Index</h2>

<script type="text/javascript">
    $(function() {
        $('#mylink').click(function() {
            $('#resultpartial1').load(this.href);
            return false;
        });
    });

    $(function() {
        $('#mysecondlink').click(function() {
            $('#resultpartial1').load(this.href, { id: 123 });
            return false;
        });
    });
</script>

<%= Html.ActionLink("click me","Partial1","Foo",new MyViewModel { Foo = "123" , Bar="456" },new { id = "mylink" }) %>
<%= Html.ActionLink("click me second link","Partial2", "Foo", new { id = "123" }, new { id = "mysecondlink" }

) %>

和这样的控制器:

public class FooController : Controller
{
    //
    // GET: /Foo/

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Partial1(string id)
    {
        // TODO: use the id to fetch the model from somewhere
        MyViewModel model = new MyViewModel { Bar = "a", Foo = "1" };
        return View(model);
    }

    public ActionResult Partial2(string id)
    {
        // TODO: use the id to fetch the model from somewhere
        MyViewModel model = new MyViewModel { Bar = "b", Foo = "2" };
        return View(model);
    }
}

}

和这样的部分视图:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

Foo: Bar:

<%@ 控制语言="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

Foo: Bar:

我总是获取控制器操作设置的值。我想在视图中设置值并传递到部分视图。我该怎么做?

I have my main view like this:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

Index

<script src="../../Scripts/jquery-1.3.2.js" type="text/javascript"></script>

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>

<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>

<h2>
    Index</h2>

<script type="text/javascript">
    $(function() {
        $('#mylink').click(function() {
            $('#resultpartial1').load(this.href);
            return false;
        });
    });

    $(function() {
        $('#mysecondlink').click(function() {
            $('#resultpartial1').load(this.href, { id: 123 });
            return false;
        });
    });
</script>

<%= Html.ActionLink("click me","Partial1","Foo",new MyViewModel { Foo = "123" , Bar="456" },new { id = "mylink" }) %>
<%= Html.ActionLink("click me second link","Partial2", "Foo", new { id = "123" }, new { id = "mysecondlink" }

) %>

and controller like this:

public class FooController : Controller
{
    //
    // GET: /Foo/

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Partial1(string id)
    {
        // TODO: use the id to fetch the model from somewhere
        MyViewModel model = new MyViewModel { Bar = "a", Foo = "1" };
        return View(model);
    }

    public ActionResult Partial2(string id)
    {
        // TODO: use the id to fetch the model from somewhere
        MyViewModel model = new MyViewModel { Bar = "b", Foo = "2" };
        return View(model);
    }
}

}

and partial views like this :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

Foo:

Bar:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

Foo:

Bar:

I am always getting values set by controller actions. I want to set values in view and pass to partial view. How can I do this ?

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

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

发布评论

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

评论(2

比忠 2024-10-25 14:04:11

假设你有一个视图模型

public class MyViewModel
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}

和一个控制器:

public class FooController: Controller
{
    public ActionResult Partial1(string id)
    {
        // TODO: use the id to fetch the model from somewhere
        MyViewModel model = ...
        return View(model);
    }

    public ActionResult Partial2(string id)
    {
        // TODO: use the id to fetch the model from somewhere
        SomeOtherViewModel model = ...
        return View(model);
    }

}

一个相应的部分视图:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.MyViewModel>" 
%>
<div>Foo: <%= Html.DisplayFor(x => x.Foo) %></div>
<div>Bar: <%= Html.DisplayFor(x => x.Bar) %></div>

最后在你的主视图中你可以有一个链接:

<%= Html.ActionLink(
    "click me", 
    "Partial1", 
    "Foo", 
    new { id = "123" }, 
    new { id = "mylink" }
) %>
<div id="resultpartial1" />

它可以是 AJAX 化的:

$(function() {
    $('#mylink').click(function() {
        $('#resultpartial1').load(this.href);
        return false;
    });
});

现在当然,如果 id 参数仅通过 javascript 知道,你可以这样做:

<%= Html.ActionLink(
    "click me", 
    "Partial1", 
    "Foo", 
    null, 
    new { id = "mylink" }
) %>

和然后:

$(function() {
    $('#mylink').click(function() {
        $('#resultpartial1').load(this.href, { id: 123 });
        return false;
    });
});

更新:

对于第二个链接:

<%= Html.ActionLink(
    "click me second link", 
    "Partial2", 
    "Foo", 
    new { id = "123" }, 
    new { id = "mysecondlink" }
) %>
<div id="resultpartial2" />

然后:

$(function() {
    $('#mysecondlink').click(function() {
        $('#resultpartial2').load(this.href, { id: 123 });
        return false;
    });
});

Assuming you have a view model

public class MyViewModel
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}

and a controller:

public class FooController: Controller
{
    public ActionResult Partial1(string id)
    {
        // TODO: use the id to fetch the model from somewhere
        MyViewModel model = ...
        return View(model);
    }

    public ActionResult Partial2(string id)
    {
        // TODO: use the id to fetch the model from somewhere
        SomeOtherViewModel model = ...
        return View(model);
    }

}

a corresponding partial view:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.MyViewModel>" 
%>
<div>Foo: <%= Html.DisplayFor(x => x.Foo) %></div>
<div>Bar: <%= Html.DisplayFor(x => x.Bar) %></div>

and finally in your main view you could have a link:

<%= Html.ActionLink(
    "click me", 
    "Partial1", 
    "Foo", 
    new { id = "123" }, 
    new { id = "mylink" }
) %>
<div id="resultpartial1" />

which could be AJAXified:

$(function() {
    $('#mylink').click(function() {
        $('#resultpartial1').load(this.href);
        return false;
    });
});

Now of course if the id parameter is known only with javascript you could do this:

<%= Html.ActionLink(
    "click me", 
    "Partial1", 
    "Foo", 
    null, 
    new { id = "mylink" }
) %>

and then:

$(function() {
    $('#mylink').click(function() {
        $('#resultpartial1').load(this.href, { id: 123 });
        return false;
    });
});

UPDATE:

And for the second link:

<%= Html.ActionLink(
    "click me second link", 
    "Partial2", 
    "Foo", 
    new { id = "123" }, 
    new { id = "mysecondlink" }
) %>
<div id="resultpartial2" />

and then:

$(function() {
    $('#mysecondlink').click(function() {
        $('#resultpartial2').load(this.href, { id: 123 });
        return false;
    });
});
你在我安 2024-10-25 14:04:11

似乎有什么问题?

包含两个链接的模式应该是(如果您使用 jQuery):

$(function(){
    // attach link clicking functionality to call controller action via Ajax
    $("#LinkID").click(function(evt) { // reference 1 below
        evt.preventDefault();
        $.ajax({
            type: "GET",
            url: $(this).attr("href"), // if link has the correct URL
            success: function(data) {
                $("#containerID").append(data);
            },
            error: function(xhr, status, err) {
                // Handle error ie. alert()
            }
        });
    });

    // add code for the second link
});

如果两个链接的工作方式相同,那么您可以更改 reference 1 行中的选择器以将两个链接包含在其中。但是,如果它们使用不同的容器,您可以将容器选择器作为自定义属性添加到链接中,并且仍然仅使用单个功能。相应地更改代码。

您可以使用通常的方式在主视图上放置链接,

<%= Html.ActionLink("Link text", "action", "controller", null, new { id = "LinkID" }) %>

这样它们就会有正确的 URL,您可以按照上面的代码使用。使用控制器操作返回您需要在客户端上显示的部分视图。

What seems to be the problem?

Pattern with both links should be (if you use jQuery):

$(function(){
    // attach link clicking functionality to call controller action via Ajax
    $("#LinkID").click(function(evt) { // reference 1 below
        evt.preventDefault();
        $.ajax({
            type: "GET",
            url: $(this).attr("href"), // if link has the correct URL
            success: function(data) {
                $("#containerID").append(data);
            },
            error: function(xhr, status, err) {
                // Handle error ie. alert()
            }
        });
    });

    // add code for the second link
});

If both links work the same, then you could change selector in line reference 1 to include both links in it. But if they work with a different container, you could add container selector as a custom attribute to the link and still use just single functionality. Change the code accordingly.

You could place links on your main view using the usual

<%= Html.ActionLink("Link text", "action", "controller", null, new { id = "LinkID" }) %>

so they will have correct URL that you can use as per code above. Use controller action that returns the partial view you need to display on the client.

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