Set null variable after method

I am creating a website and I need to show a list of customers. When a client is selected to show its elements, I send the client identifier from the view as follows:

 <th>
 @using (Html.BeginForm("SetID", "Client", FormMethod.Post, new 
{ id = item.id.ToString() }))
   {
    @Html.Hidden("id", item.id.ToString());
    <input type="submit" value="see items" />
   }
   </th>

I get the identifier in the controller and save it to make a request and show the values ​​in this way.

private string customer_id
[HttpPost]
public ActionResult SetCustomer(string id) {
    Save(id);
    return RedirectToAction("Index");
}

private void Save(string id) {
    this.customer_id = id;
}

But when I redirect to the Index view, the variable "customer_id" is null. Is something missing?

+4
source share
2 answers

Because you do not save the value anywhere.

HTTP . ASP.NET , HTTP- . , :

  • SetCustomer,
  • ,
  • Index,

, . , , , . , , :

  • Session
  • URL
  • Cookie
  • .

, - , . -.


. ASP.NET MVC :

return RedirectToAction("Index", new { customer_id = id });

, URL a customer_id, . , Index :

ActionResult Index(int? customer_id = null)
{
    // use the customer id if one is provided
}

, , Index . , , . , , .

, -. ( customer_id) /, (, ..), .

+3

ASP.NET MVC- . this.customer_id . - , - .

, , , , ( , ).

, - , , , . DI IOC , .

0

Source: https://habr.com/ru/post/1674690/


All Articles