Persisting values across multiple controller actions

风流意气都作罢 提交于 2019-12-20 00:47:15

问题


In my controller i have an ID to an object in a database, and multiple actions needs this id to access the object. This ID is fetched in the Index action and used to populate the view model. The way I get the ID is through an argument, which EPiServer (out CMS) passes the Index action, so I cant really get it anywhere else.

My question is, is there any way to make this id accessible to my actions without the use a hidden field etc on the client side?

Id like to avoid sending extra data as much as possible, and it isnt really a good solution.


回答1:


I'm not sure what you mean by "it isn't really a good solution," since using hidden fields is potentially the cleanest method of persisting data across multiple posts. Bear in mind that HTTP is - and should be treated as - a stateless protocol. A few other options to consider, however:

Cookies

Add the value to a cookie in your controller and add it to the Response object. Then, pick it up and read it as and when you need it using Request.Cookies.

/* Setting */
Response.Cookies["id"].Value = "myId";

/* Getting */
var id = Request.Cookies["id"] != null
    ? Request.Cookies["id"].Value
    : String.Empty;

Pros: Persist across multiple requests without having to do anything in the requests in between.

Cons: Easily switched off client-side.

Session state

Add the value to session state using the Session collection.

/* Setting */
Session["id"] = "myId";

/* Getting */
var myId = Session["id"] != null ? Session["id"].ToString() : String.Empty;

Pros: Easy to use, persist across multiple requests

Cons: Can get very messy if the user has multiple tabs open, uses the 'Back' button, etc. Often unpredictable.

Hidden fields

Add the value to a hidden field and pick this value back up on the next POST.

/* View (assuming string model property 'Id') */
@Html.BeginForm("MyAction", "MyController")
{
    @Html.HiddenFor(m => m.Id)
    <input type="submit" value="Submit" />
}

/* Controller */
public ActionResult MyAction(MyModel model)
{
    var id = model.Id;
}

Pros: No footprint. Clean, simple, stateless and efficient.

Cons: No good for persisting across GET requests. Only persists across one POST at a time.

Query string

Add the values as query string parameters on redirects.

/* View */
@Html.ActionLink("Go!", "MyAction", new { identifier = "myId" })

/* Controller */
public ActionResult MyAction(string identifier)
{
    var id = identifier ?? String.Empty;
}

Pros: Unlike hidden fields, can be used for GET requests.

Cons: Requires you to implement a method of persisting the query string across all requests for which you need the values.

What's best?

Personally, I'd say that - in your case - using cookies is probably the best option. If you want to persist across multiple requests without having to handle the value in every single request (and without the horribleness of using Session), cookies are likely to be the simplest and neatest approach.




回答2:


I usually go for hidden input in form for a scenario like this but if you are unwilling, You can use Cookies or Sessions.



来源:https://stackoverflow.com/questions/16187983/persisting-values-across-multiple-controller-actions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!