开发者

Convert implicitly typed object to querystring

开发者 https://www.devze.com 2023-03-04 03:57 出处:网络
In ASP.NET MVC3, some function, like HtmlHelper.ActionLink, can take in an implicitly typed object and convert it into an querystring

In ASP.NET MVC3, some function, like HtmlHelper.ActionLink, can take in an implicitly typed object and convert it into an querystring

@Html.ActionLink("开发者_如何学GoLink", "Action", new { id = 1, params="asd"})

Will result in an url like http://www.localhost.com/controller/Action?id=1&params=asd

Is there a built-in method to convert the properties of an object to a querystring format?


Assuming you have a view model:

public class MyViewModel
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

and a controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Prop1 = "foo",
            Prop2 = "bar"
        };
        return View(model);
    }
}

you could use the following overload in your view:

@model MyViewModel
@Html.ActionLink("Link", "Action", new RouteValueDictionary(Model))

in your view.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号