我可以将视图模型传递给操作链接来生成路线吗?

发布于 2024-12-09 13:33:46 字数 1748 浏览 1 评论 0原文

我需要创建一个基于我的搜索条件的链接。例如:

localhost/Search?page=2&Location.PostCode=XX&Location.Country=UK&IsEnabled=true 

此链接中的参数是我的 SearchViewModel 中的属性值。

理想情况下,我希望有以下内容:

@Html.ActionLink("Search","User", Model.SearchCriteria)

这是默认支持的还是我需要将视图模型的属性传递到 RouteValueDictionary 类型对象中然后使用它?

我的目标是编写一个分页助手,它将生成页码并将搜索条件参数附加到生成的链接中。

例如,

@Html.GeneratePageLinks(Model.PagingInfo, x => Url.Action("Index"), Model.SearchCriteria)

我将您的解决方案与 PRO ASP.NET MVC 3 书中的建议相结合,最终得到以下结果:

用于生成链接的帮助程序。有趣的部分是 pageUrlDelegate 参数,稍后用于调用 Url.Action 来生成链接:

public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfoViewModel pagingInfo, 
                                        Func<int,String> pageUrlDelegate)
{
    StringBuilder result = new StringBuilder();
    for (int i = 1; i <= 5; i++)
    {
        TagBuilder tagBuilder = new TagBuilder("a");                                
        tagBuilder.MergeAttribute("href", pageUrlDelegate(i));
        tagBuilder.InnerHtml = i.ToString();
        result.Append(tagBuilder.ToString());               
    }            

    return MvcHtmlString.Create(result.ToString());
}   

然后在视图模型中:

@Html.PageLinks(Model.PagingInfo, x => Url.Action("Index","Search", new RouteValueDictionary()
    {
            { "Page", x },
            { "Criteria.Location.PostCode", Model.Criteria.Location.PostCode },   
            { "Criteria.Location.Town", Model.Criteria.Location.Town},
            { "Criteria.Location.County", Model.Criteria.Location.County}
    }))
)

我仍然对字符串中的属性名称不满意,但现在必须这样做。

谢谢 :)

I need to create a link that will be based on my search criteria. For example:

localhost/Search?page=2&Location.PostCode=XX&Location.Country=UK&IsEnabled=true 

Parameters in this link are the values of properties in my SearchViewModel.

Ideally I'd like to have something on the lines of:

@Html.ActionLink("Search","User", Model.SearchCriteria)

Is this supported by default or do I need to pass properties of my view model into RouteValueDictionary type object and then use that?

My goal is to write a paging helper which would generate page numbers and append the search criteria parameters to the generated links.

E.g.

@Html.GeneratePageLinks(Model.PagingInfo, x => Url.Action("Index"), Model.SearchCriteria)

I've combined your solutions with suggestion from PRO ASP.NET MVC 3 book and ended up with the following:

Helper for generating links. Interesting part is pageUrlDelegate parameter which is later used to invoke Url.Action for generating links:

public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfoViewModel pagingInfo, 
                                        Func<int,String> pageUrlDelegate)
{
    StringBuilder result = new StringBuilder();
    for (int i = 1; i <= 5; i++)
    {
        TagBuilder tagBuilder = new TagBuilder("a");                                
        tagBuilder.MergeAttribute("href", pageUrlDelegate(i));
        tagBuilder.InnerHtml = i.ToString();
        result.Append(tagBuilder.ToString());               
    }            

    return MvcHtmlString.Create(result.ToString());
}   

Then in the view model:

@Html.PageLinks(Model.PagingInfo, x => Url.Action("Index","Search", new RouteValueDictionary()
    {
            { "Page", x },
            { "Criteria.Location.PostCode", Model.Criteria.Location.PostCode },   
            { "Criteria.Location.Town", Model.Criteria.Location.Town},
            { "Criteria.Location.County", Model.Criteria.Location.County}
    }))
)

I'm still not happy with property names in Strings, but It'll have to do for now.

Thank you :)

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

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

发布评论

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

评论(2

佞臣 2024-12-16 13:33:46

理想情况下,我想要以下内容:

@Html.ActionLink("搜索","用户", Model.SearchCriteria)

不幸的是,这是不可能的。您必须一一传递属性。您确实可以使用带有 RouteValueDictionary 的重载:

@Html.ActionLink(
    "Search",
    "User",
    new RouteValueDictionary(new Dictionary<string, object>
    {
        { "Location.PostCode", Model.SearchCriteria.PostCode },
        { "Location.Country", Model.SearchCriteria.Country },
        { "IsEnabled", Model.IsEnabled },
    })
)

当然,最好编写一个自定义 ActionLink 帮助器来执行此操作:

public static class HtmlExtensions
{
    public static IHtmlString GeneratePageLink(this HtmlHelper<MyViewModel> htmlHelper, string linkText, string action)
    {
        var model = htmlHelper.ViewData.Model;
        var values = new RouteValueDictionary(new Dictionary<string, object>
        {
            { "Location.PostCode", model.SearchCriteria.PostCode },
            { "Location.Country", model.SearchCriteria.Country },
            { "IsEnabled", model.IsEnabled },
        });
        return htmlHelper.ActionLink(linkText, action, values);
    }
}

然后:

@Html.GeneratePageLink("some page link text", "index")

另一种可能性是仅传递 id > 并让控制器操作从您最初在渲染此视图的控制器操作中获取模型和值的位置获取相应的模型和值。

Ideally I'd like to have something on the lines of:

@Html.ActionLink("Search","User", Model.SearchCriteria)

Unfortunately that's not possible. You will have to pass properties one by one. You could indeed use the overload which takes a RouteValueDictionary:

@Html.ActionLink(
    "Search",
    "User",
    new RouteValueDictionary(new Dictionary<string, object>
    {
        { "Location.PostCode", Model.SearchCriteria.PostCode },
        { "Location.Country", Model.SearchCriteria.Country },
        { "IsEnabled", Model.IsEnabled },
    })
)

Of course it's probably better to write a custom ActionLink helper to do this:

public static class HtmlExtensions
{
    public static IHtmlString GeneratePageLink(this HtmlHelper<MyViewModel> htmlHelper, string linkText, string action)
    {
        var model = htmlHelper.ViewData.Model;
        var values = new RouteValueDictionary(new Dictionary<string, object>
        {
            { "Location.PostCode", model.SearchCriteria.PostCode },
            { "Location.Country", model.SearchCriteria.Country },
            { "IsEnabled", model.IsEnabled },
        });
        return htmlHelper.ActionLink(linkText, action, values);
    }
}

and then:

@Html.GeneratePageLink("some page link text", "index")

Another possibility is to pass only the id and have the controller action fetch the corresponding model and values from wherever you fetched it initially in the controller action that rendered this view.

趁微风不噪 2024-12-16 13:33:46

以下是从 Steve Sanderson 的 Tekpub 示例 中获取的一些代码,大致就是您要查找的内容为了。首先在您的对象上定义一个扩展方法 -

public static RouteValueDictionary DetailsRouteValues(this JobAd jobAd)
{
    return new RouteValueDictionary
    {
        { "controller", "Jobs" },
        { "action", "Details" },
        { "id", jobAd.JobAdId },
        { "country", MakeSimpleUrlSegment(jobAd.LocationCountry) },
        { "city", MakeSimpleUrlSegment(jobAd.LocationCity) },
        { "title", MakeSimpleUrlSegment(jobAd.Title) }
    };
}

然后使用该方法调用 Html.RouteLink -

<%= Html.RouteLink(ad.Title, ad.DetailsRouteValues())%>

Here's some code taken from Steve Sanderson's Tekpub samples that is broadly what you're looking for. First define an extension method on your object -

public static RouteValueDictionary DetailsRouteValues(this JobAd jobAd)
{
    return new RouteValueDictionary
    {
        { "controller", "Jobs" },
        { "action", "Details" },
        { "id", jobAd.JobAdId },
        { "country", MakeSimpleUrlSegment(jobAd.LocationCountry) },
        { "city", MakeSimpleUrlSegment(jobAd.LocationCity) },
        { "title", MakeSimpleUrlSegment(jobAd.Title) }
    };
}

Then call Html.RouteLink using that method -

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