2010-05-10 93 views
0

用戶控制asp.net的MVC用戶控制問題

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 

<% 
    using (MvcApplication1.Models.EgovtDataContext _model = new MvcApplication1.Models.EgovtDataContext()) 
    { 
%> 

    <% foreach(var md in MvcApplication1.Models.) { %> 
     <% if (md.ParentId == 0) 
      { %> 

     <% } %> 
    <% } %> 
    <%} %> 

FeatureRepository類

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace MvcApplication1.Models 
{ 
    public class FeaturesRepository 
    { 
     EgovtDataContext db = new EgovtDataContext(); 

     public IEnumerable<Feature> GetAllFeatures() 
     { 
      List<Feature> Features = new List<Feature>(); 
      Feature feature = new Feature(); 


      var AllParentFeatures = FeaturesByParentId(0); 
      foreach (Feature frParentCategory in AllParentFeatures) 
      { 
       feature.int_FeatureId = frParentCategory.int_FeatureId; 

       feature.vcr_FeaturesName = frParentCategory.vcr_FeaturesName; 
       feature.int_ParentId = frParentCategory.int_ParentId; 
       Features.Add(feature); 


       var AllChildFeaturesofthatParent = FeaturesByParentId(frParentCategory.int_FeatureId); 
       int AllChildFeaturesofthatParentCount = AllChildFeaturesofthatParent.Count(); 
       foreach (Feature frChildCategory in AllChildFeaturesofthatParent) 
       { 
        feature.int_FeatureId = frParentCategory.int_FeatureId; 

        feature.vcr_FeaturesName = frParentCategory.vcr_FeaturesName; 
        feature.int_ParentId = frParentCategory.int_ParentId; 
        Features.Add(feature); 
       } 

      } 
      return Features; 

     } 

    } 
} 

1)此正確地進行公開的IEnumerable GetAllFeatures()在上面的方法?,並且該方法正確地寫入? 2)我想調用上面的控件中的GetAllFeatures方法?我會怎麼做? 3)用戶控制器應該在共享文件夾中創建?什麼是共享文件夾?

回答

5

創建一個控制器動作

public ActionResult All() 
{ 
    return View(new FeaturesRepository().GetAllFeatures()); 
} 

,改變你的用戶控件聲明

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Feature>>" %> 


<% foreach(var md in Model) 
    { %> 
     <% if (md.ParentId == 0) 
      { %> 
       //blabla 
     <% } %> 
<% } %> 
1

您需要在控制器中創建此存儲庫類的實例,並使用該實例調用返回IList的所有功能並訪問該模型中Ilist的方法。

+0

用戶控制器應共享文件夾中創建? – maztt 2010-05-10 19:45:05

+0

您可以在控制器文件夾中創建控制器 – Pinu 2010-05-10 19:53:12

1

上述方法看起來不錯,但我不知道您的業務規則的具體情況。只是一些代碼語法修改會很好(如使用大寫字母F的功能,是錯誤的外殼)等。

關於通話,您不能只從視圖或查看用戶控件調用上述方法。在渲染視圖之前,必須使用此方法的數據填充ViewData或Model。另一種方法是使用JavaScript,特別是JSON調用,以從返回JsonResult或ContentResult或類似內容的操作中請求數據。

控制器操作應典型地返回ActionResult或從其繼承的類型(ContentResult,JsonResult,RedirectToActionResult等)。