Asp.net mvc 获取部分视图的Ajax请求
我的主要观点如下:Asp.net mvc 获取部分视图的Ajax请求,asp.net-mvc,ajax,partial-views,Asp.net Mvc,Ajax,Partial Views,我的主要观点如下: 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 so
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);
}
}
指数
}
部分观点如下:
傅:
酒吧:
傅:
酒吧:
我总是通过控制器动作设置值。我想在视图中设置值并传递到局部视图。我该怎么做?有什么问题吗 带有两个链接的模式应该是(如果使用jQuery): 如果两个链接的工作方式相同,则可以更改线参照1中的选择器,使其包含两个链接。但是如果它们使用不同的容器,您可以将容器选择器作为自定义属性添加到链接中,并且仍然只使用单个功能。相应地更改代码 您可以使用通常的方法在主视图上放置链接
<%= Html.ActionLink("Link text", "action", "controller", null, new { id = "LinkID" }) %>
因此,他们将有正确的URL,您可以按照上面的代码使用。使用控制器操作返回您需要在客户端上显示的部分视图。假设您有一个视图模型
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>
当然,如果id参数仅在javascript中已知,则可以执行以下操作:
<%= Html.ActionLink(
"click me",
"Partial1",
"Foo",
null,
new { id = "mylink" }
) %>
更新: 至于第二个环节:
<%= Html.ActionLink(
"click me second link",
"Partial2",
"Foo",
new { id = "123" },
new { id = "mysecondlink" }
) %>
<div id="resultpartial2" />
有什么解决方案/提示吗?到目前为止你有什么建议?你能发布一些代码吗?我有一个视图,我需要两个链接。我有两个部分视图和一个模型类。这些链接应该是什么样子的呢?在《如何给出ajax将使用的id》中?@Darin:把它放在哪里:@model-AppName.Models.MyViewModel-Foo:@Html.DisplayFor(x=>x.Foo)Bar:@Html.DisplayFor(x=>x.Bar)@asif,我已经修改了我的代码,以使用您没有指定的WebForms视图引擎。如果您使用的是WebForms视图引擎,则将此代码放在
~/Views/Foo/Partial1.ascx
中;如果您使用的是Razor,则将此代码放在~/Views/Foo/Partial1.cshtml
中。@asif,介意分享什么错误或您希望我读懂您的想法吗?'System.Web.Mvc.HtmlHelper'不包含'DisplayFor'的定义,并且找不到扩展方法'DisplayFor'接受类型为'System.Web.Mvc.HtmlHelper'的第一个参数(是否缺少using指令或程序集引用?@asif,然后试着这样做:
但是Html.DisplayFor
应该可以工作。您使用的是什么版本的MVC?请在新创建的项目中从头开始尝试。
<%= Html.ActionLink(
"click me",
"Partial1",
"Foo",
new { id = "123" },
new { id = "mylink" }
) %>
<div id="resultpartial1" />
$(function() {
$('#mylink').click(function() {
$('#resultpartial1').load(this.href);
return false;
});
});
<%= 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;
});
});