簡體   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