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¶ms=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.
精彩评论