2012-11-21 18 views
0

我有一個視圖在我的mvc Intranet中創建進程。我需要獲取數據到我的視圖

在該視圖中,我有一個需要填充數據的表。

這是我的看法

@model TestingTool.ViewModels.ProcessModel 
@{ 
    ViewBag.Title = "Create"; 
} 
<h2> 
    Create</h2> 
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>Process</legend> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Name) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Name) 
      @Html.ValidationMessageFor(model => model.Name) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Code) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Code) 
      @Html.ValidationMessageFor(model => model.Code) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.Description) 
     </div> 
     <div class="editor-field"> 
      @Html.TextAreaFor(model => model.Description) 
      @Html.ValidationMessageFor(model => model.Description) 
     </div> 
     <table> 
      <thead> 
       <tr> 
        <th> 
         Region 
        </th> 
        <th> 
         Owner 
        </th> 
       </tr> 
      </thead> 
      <tbody> 
       @if (Model != null) 
       { 
        foreach (var item in ViewBag.Regions) 
        { 
         <tr> 

          <td> 
          @Html.DisplayTextFor(i => item.Name) 
          </td> 

          <td> 
           @Html.DropDownListFor(i => item.User, new SelectList(item.User, "Id", "Name")) 
          </td> 

         </tr> 
        } 
       } 
      </tbody> 
     </table> 
     <p> 
      <input type="button" value="Create" /> 
     </p> 
    </fieldset> 
} 
<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

這是我的控制器

// 
     // GET: /Process/Create 

     public ActionResult Create() 
     { 
      Collection<RegionModel> region = new Collection<RegionModel>(); 

      foreach (var regions in _db.Regions) 
      { 
       if (regions.Name != "Europe") 
       { 
        RegionModel iRegion = new RegionModel 
         { 
          Name = regions.Name, 
          Id = regions.Id, 
          User = new Collection<UserModel>() 
         }; 

        foreach (User user in _db.Users) 
        { 
         UserModel iUser = new UserModel { Id = user.Id, Name = user.Name }; 

         iRegion.User.Add(iUser); 
        } 

        region.Add(iRegion); 
       } 
      } 

      ViewBag.Regions = region; 

      return View(); 
     } 

我認爲這是可能要迭代的ViewBag,但我得到這個錯誤

An expression tree may not contain a dynamic operation - mvc 

而且我想知道的另一件事是,如果有可能有一個模型預先填充數據。

回答

1

請勿將VIEWBAG用於模型數據。創建一個視圖模型屬性,它有一個點。

class MyViewModel { 
    public Collection<RegionModel> regions {get;set;} 
    ...snip... 
} 

然後在你的控制器:

var viewmodel = new MyViewModel(); 
viewmodel.regions = region; 

return View(viewmodel); 

編輯: 看起來你已經有一個視圖模型:@model TestingTool.ViewModels.ProcessModel 你能屬性添加到該區域的視圖模型?

+0

有趣的是,我以前試過,它工作得很好,我在視圖中獲取數據,但是當我點擊「創建」時,沒有任何反應,我的意思是什麼都沒有。 –

+0

你有控制器方法來接受表單文章嗎? –

+0

某些奇怪的原因我不明白,現在正在工作。我使用Json將數據發送給控制器。就像它開始工作。 Tks也一樣。 –

相關問題