asp.net mvc 4 - Return View() or PartialView()? How to decide? -
i hava action:
public actionresult getorders(int id) { ... }
when access through hyperlink(~/order/getorders/1), want getorder return view()
, whole page.
when through @html.action("getorders")
, want return partialview() part of page.
now settled problem using erik philips's method.
public actionresult getorders(int id) { var orders = db.order.where(a => a.adcompanyid == id).tolist(); viewbag.adcompanyname = db.adcompany.where(a => a.id == id).select(a => a.name).first().tostring(); if (controllercontext.ischildaction) { viewbag.ispartial = true; return partialview(orders); } viewbag.ispartial = false; return view(orders); } @{html.renderaction("getorders", new { id = model.id });} @html.actionlink("related orders", "getorders", new { id = item.id })
in getorders.cshtml:
@if (viewbag.ispartial == false) { ... }
to generate different view.
queti m. porta same!
you can use controllercontext.ischildaction
.
public actionresult foo() { if (controllercontext.ischildaction) { return partialview("getorderspartial", model); } return view("getorders", model); }
also, recommend using html.renderaction
.
updated per comment
i'd mention i've never had need this, in own experience. either have different view, or unaware partialview return view without layout.
Comments
Post a Comment