Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在asp.net mvc中使用json时,Foreach不工作_Asp.net Mvc_Json_Foreach - Fatal编程技术网

Asp.net mvc 在asp.net mvc中使用json时,Foreach不工作

Asp.net mvc 在asp.net mvc中使用json时,Foreach不工作,asp.net-mvc,json,foreach,Asp.net Mvc,Json,Foreach,大家好, 我正在尝试使用json。如果返回到局部视图,我不能将foreach用于我的客户会话。我的客户会话有客户。我无法列出他们。 我想念哪里 CONTROLLER: public ActionResult ShowResult(MyModel model) { Session["CustomerList"] = context.Customer.Where(s => s.CustomerSituation== true).ToList(); // Customers List To S

大家好,

我正在尝试使用json。如果返回到局部视图,我不能将foreach用于我的客户会话。我的客户会话有客户。我无法列出他们。 我想念哪里

CONTROLLER:

public ActionResult ShowResult(MyModel model)
{
Session["CustomerList"] = context.Customer.Where(s => s.CustomerSituation== true).ToList(); // Customers List To Session
var stringView = RenderRazorViewToString("_ShowResultPartial", model);
return Json(stringView, JsonRequestBehavior.AllowGet);
}

My\u ShowResultPartial视图:
@foreach(列表会话[“CustomerList”]中的var项)
{
ActionLink(item.CustomerName,“ShowResult”,新的{CustomerId=item.CustomerId},新的AjaxOptions{HttpMethod=“POST”});
}

根据您发布的内容,不清楚您为什么要在会话中存储客户列表;视图的数据通常应存储在视图模型上。即使您有充分的理由使用会话,也最好在控制器中检索会话变量并将其存储在视图模型中。然后,您应该能够从视图中循环浏览模型上的列表。在这种情况下,看起来根本不需要会话(除非您打算稍后重用存储的数据,并且由于某种原因无法通过模型传递数据)

另外,除非有很好的理由返回json,否则ShowResult控制器方法应该只返回PartialView

像这样的东西应该有用

控制器:

public ActionResult ShowResult(MyModel model)
{
    model.Customers = context.Customer.Where(s => s.CustomerSituation == true).ToList();
    return PartialView("_ShowResultPartial"), model);
}
局部视图:

@model MyModel

@foreach (var item in Model.Customers)
{
    Ajax.ActionLink(item.CustomerName, "ShowResult", new { CustomerId = item.CustomerId}, new AjaxOptions { HttpMethod = "POST" });
}
@model MyModel

@foreach (var item in Model.Customers)
{
    Ajax.ActionLink(item.CustomerName, "ShowResult", new { CustomerId = item.CustomerId}, new AjaxOptions { HttpMethod = "POST" });
}