Answered by:
Persisting query string

Question
-
User965740324 posted
What I've got is a MVC page that lists a bunch of things, let's say orders. The page can be filtered by "customer" or "item" by setting a parameter/value in the query string, the index controller will handle as needed. You can also click a link to create a new order. What I want to do is pass the query string from the index to the create (if it is present). Sometimes the "order" view is filtered by customer, sometime by item, so when I create new, I want a similar filter.
I thought I could do this
@Html.ActionLink("Create New", "Create", "Orders", Context.Request.QueryString)
But the query string gets encoded so it does not work.
Monday, November 16, 2020 11:37 PM
Answers
-
User-474980206 posted
to use the html helper, it requires the query string to be an object. to convert a query string to object will require code generation. but this is the hard way. just use html:
<a href="@(Url.Action("Create", "Orders") + "?" + Context.Request.QueryString)">Create New</a>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 17, 2020 4:27 PM
All replies
-
User-1151440187 posted
You can add directly like this.
Hope it helps.
@Html.ActionLink("Create New", "Create", "Orders", new{id:2,name:""});
Tuesday, November 17, 2020 4:25 AM -
User1686398519 posted
Hi wild_bill,
You can pass parameters like this:
@Html.ActionLink("Create New One", "Create", "PageList", new { querystring = Context.Request.QueryString }, null)
Or
@Html.ActionLink("Create New Two", "Create", new { querystring = Context.Request.QueryString })
Create
public ActionResult Create(string querystring) { return View(); }
Here is the result.
Best Regards,
YihuiSun
Tuesday, November 17, 2020 5:40 AM -
User965740324 posted
Thanks for the reply, but rather than create a new query string with query string as a parameter, do you know if I can just keep the original query string?
Tuesday, November 17, 2020 3:28 PM -
User965740324 posted
The issue is there won't always by "id". Sometimes there will be customerid, sometimes itemid, other times blank.
Tuesday, November 17, 2020 3:29 PM -
User-474980206 posted
to use the html helper, it requires the query string to be an object. to convert a query string to object will require code generation. but this is the hard way. just use html:
<a href="@(Url.Action("Create", "Orders") + "?" + Context.Request.QueryString)">Create New</a>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 17, 2020 4:27 PM -
User475983607 posted
The issue is there won't always by "id". Sometimes there will be customerid, sometimes itemid, other times blank.
But, your action method will always have named customerId and itemid input parameters.
Tuesday, November 17, 2020 4:29 PM -
User965740324 posted
Correct, the issue is passing the parameters received to the action method.
Tuesday, November 17, 2020 5:32 PM -
User475983607 posted
wild_bill
Correct, the issue is passing the parameters received to the action method.
Bruce's example code does exactly what you asked. But does it solve the actual problem? How do you know when the querystring can be passed to a particular URL? Do all your action methods accept the same parameters?
Maybe you need to define a model that contains all the possible name/values?
Tuesday, November 17, 2020 5:46 PM -
User965740324 posted
Thank you Bruce!
Wednesday, November 18, 2020 7:48 PM