2016-05-17 38 views
0

我想获取数据并显示每个循环内的数据。请在MVC中提供示例代码完整的新手。是否有任何方法调用在Mvc Foreach循环内的方法

@foreach (var item in Model) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.cntryName) 
       //---- Here I want to call another method based on contryId and bind details 
       in another table 

      </td> 
+0

您并使用ajax并调用您的控制器,然后将结果解析到您的视图。一些例子:http://www.binaryintellect.net/articles/218ca630-ba50-48fe-af6e-6f754b5894aa.aspx – freshbm

+0

http://stackoverflow.com/questions/22843324/calling-controller-action-method-directlyrom -razor-view – NnN

回答

1

可以编写的HtmlHelper扩展

using System.Web.Mvc; 
namespace WebApplication1 
{ 
    public static class HtmlHelperExtensions 
    { 
     public static string YourTableEmittingMethod(this HtmlHelper helper, int countryId) 
     { 
     //Add your logic to create html string using the countryId 
     //and return string containing the Html tags for the table 
     } 
    } 
} 

,那么你可以在你的MVC视图中使用此方法,因为@Html.YourTableEmittingCode(@item.CountryId)

了解更多关于HtmlExtensions here

+0

感谢您指点我正确的方向。我会做剩下的。 –

0

为什么不返回所有模型中需要的数据?

从发送到视图的模型中,执行循环,以便返回到视图的模型具有所需的所有数据。在您的cs文件中编写您的逻辑代码比在您的视图中编写要好得多。

相关问题