微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MVC 3 Razor的Master-Detail示例代码(使用Ajax了解详细信息)

我正在寻找示例代码来创建一个主/细节与c#mvc 3.

具体来说,我试图找出如何通过ajax调用部分视图的渲染.我可以把部分视图放在窗体上,但是想要在用户通过ajax从选择列表中选择一个项目之后进行填充.

谢谢

与往常一样,您从模型开始:
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);
    }
}

和相应的看法.

〜/查看/主页/ 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>

〜/查看/主页/ Details.cshtml:

@model Detailsviewmodel
@Model.Foo
@Model.Bar

〜/浏览/首页/ displayTemplates / Myviewmodel.cshtml:

@model Myviewmodel
<li>
    @Html.ActionLink(Model.Title,"details",new { id = Model.Id },new { @class = "detailsLink" })
</li>

原文地址:https://www.jb51.cc/ajax/159874.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐