繁体   English   中英

.net MVC和url.Action与marging匿名

[英].net MVC and url.Action with marging anonymous

我尝试给2个匿名对象添加边距,并将结果传递给urlHelper.action()方法,但结果错误,

这是我的代码:

public static MvcHtmlString EditButton(this HtmlHelper helper, object routeValues = null, string text = "")
    {
        UrlHelper u = new UrlHelper(helper.ViewContext.RequestContext);

        routeValues = routeValues ?? new { };

        string returnUrl = (string)helper.ViewBag.returnUrl;

        if (false == string.IsNullOrEmpty(returnUrl))
            routeValues = ButtonHelper.Merge(routeValues, new { returnUrl = returnUrl });

        var x = u.RouteUrl(new RouteValueDictionary(routeValues));
        string href = u.Action("Edit",new RouteValueDictionary(routeValues));//<--here I aspect that href=".../edit/123?returnUrl=...."
        text = string.IsNullOrEmpty(text) ? Resources.Commons.Edit : text;

        //        < a href = '@Url.Action("Edit", new { id = Model.Id })' class="btn btn-default">
        //    <i class="glyphicon glyphicon-pencil"></i> @Resources.Commons.Edit
        //</a>
        MvcHtmlString r = new MvcHtmlString(String.Format("<a href=\"{0}\" class=\"btn btn-default\">"
            + "<i class=\"glyphicon glyphicon-pencil\"></i> {1}</a>", href, text));
        return r;
    }

    public static dynamic Merge(dynamic item1, dynamic item2)
    {
        var result = new ExpandoObject();
        var d = result as IDictionary<string, object>; //work with the Expando as a Dictionary

        foreach (System.Reflection.PropertyInfo fi in item1.GetType().GetProperties())
        {
            d[fi.Name] = fi.GetValue(item1, null);

        }
        foreach (System.Reflection.PropertyInfo fi in item2.GetType().GetProperties())
        {
            d[fi.Name] = fi.GetValue(item2, null);

        }

        return d;

}

有什么问题,我将href变量表示为:“ ... / edit / 123?returnUrl = ....”

谢谢你的问候

好吧,我解决了,我修改Marge方法的新代码是:

        public static RouteValueDictionary Merge(dynamic item1, dynamic item2)
        {
            var result = new ExpandoObject();
            var d = new RouteValueDictionary();

            foreach (System.Reflection.PropertyInfo fi in item1.GetType().GetProperties())
            {
                d[fi.Name] = fi.GetValue(item1, null);

            }
            foreach (System.Reflection.PropertyInfo fi in item2.GetType().GetProperties())
            {
                d[fi.Name] = fi.GetValue(item2, null);

            }

            return d;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM