我要找的代碼示例創建與C#MVC主/細節3.主詳細示例代碼
具體來說,我試圖找出如何調用通過ajax呈現局部視圖。我能夠將部分視圖放在窗體上,但是希望在用戶通過ajax從選擇列表中選擇一個項目後填充它。
THX
我要找的代碼示例創建與C#MVC主/細節3.主詳細示例代碼
具體來說,我試圖找出如何調用通過ajax呈現局部視圖。我能夠將部分視圖放在窗體上,但是希望在用戶通過ajax從選擇列表中選擇一個項目後填充它。
THX
像往常一樣啓動與模型:
public class MyViewModel
{
public int Id { get; set; }
public string Title { get; set; }
}
public class DetailsViewModel
{
public string Foo { get; set; }
public string Bar { get; set; }
}
然後控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
// TODO: don't hardcode, fetch from repository
var model = Enumerable.Range(1, 10).Select(x => new MyViewModel
{
Id = x,
Title = "item " + x
});
return View(model);
}
public ActionResult Details(int id)
{
// TODO: don't hardcode, fetch from repository
var model = new DetailsViewModel
{
Foo = "foo detail " + id,
Bar = "bar detail " + id
};
return PartialView(model);
}
}
和相應的視圖。
~/Views/Home/Index.cshtml
:
@model IEnumerable<MyViewModel>
<ul>
@Html.DisplayForModel()
</ul>
<div id="details"></div>
<script type="text/javascript">
$(function() {
$('.detailsLink').click(function() {
$('#details').load(this.href);
return false;
});
});
</script>
~/Views/Home/Details.cshtml
:
@model DetailsViewModel
@Model.Foo
@Model.Bar
~/Views/Home/DisplayTemplates/MyViewModel.cshtml
:
@model MyViewModel
<li>
@Html.ActionLink(Model.Title, "details", new { id = Model.Id }, new { @class = "detailsLink" })
</li>
這很好。然而,我確實有跟進問題/問題。所以我在#details部分基於treeview上的選擇加載了部分視圖。這工作。 目前,用戶在#details部分點擊「保存」後發生的情況是,整個頁面已加載,並顯示「保存」。幫幫我? 在#details部分,我將部分視圖與表單配合使用。當用戶點擊「保存」時,我想再次在#details部分放置「成功保存」。但請注意,此表單來自ajax響應,現在我希望在#details中提供另一個來自ajax的響應。 – David 2011-04-07 02:21:43
我blogged關於使用asp.net的MVC,您可以添加n個子創建主從表單唱片在clietn一邊不需要發送ajax請求來爲子記錄帶上編輯器字段。它使用jQuery模板
你可以發佈你到目前爲止的代碼嗎? – gideon 2011-04-04 04:55:27