2012-02-06 63 views
0

我按照這個例子,使N至N關係如何獲取視圖的文本框?在我的控制器

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

它做工精細,但對於N到N的關係與有效載荷到數據庫中,我找出我可以做[HTTPGET]並顯示我想要顯示的視圖,但現在我想知道如何獲得我在視圖中的文本框,我可以在我的控制器中看到複選框(請參閱下面的操作),這是我的視圖,所以我的問題將如何獲得文本框?在我的每個複選框的控制器?

@using (Html.BeginForm("AgregarEmpresas", "Empleado")) 
{ 

    <fieldset> 
<div class="editor-field"> 
    <table> 
     <tr> 
      @{ 
       int cnt = 0; 
       List<ITCOrganigrama.ViewModel.AsignarEmpresa> empresas = ViewBag.Empresas; 

       foreach (var empresa in empresas) 
       { 
        if (cnt++ % 5 == 0) { 
         @: </tr> <tr> 
        } 
        @: <td> 
         <input type="checkbox" 
           name="selectedEmpresa" 
           value="@empresa.EmpresaId" 
           @(Html.Raw(empresa.Assigned ? "checked=\"checked\"" : "")) /> 
          @empresa.Nombre 
         <div> 
          @Html.LabelFor(model => empresa.cargo) 
          @Html.TextBoxFor(model => empresa.cargo, new { style = "width: 150px;" }) 
          @Html.ValidationMessageFor(model => empresa.cargo) 
         </div> 
        @:</td> 
       } 
       @: </tr> 
      } 
    </table> 
</div> 
<p> 
      <input type="submit" value="Agregar" /> 
     </p> 
</fieldset> 
} 

在那裏我得到chekbox行動

[HttpPost] 
     public ActionResult AgregarEmpresas(int? id, FormCollection formCollection, string[] selectedEmpresa) 
     { 
} 

我最後的觀點: http://s3.subirimagenes.com:81/otros/previo/thump_7406511add1.jpg http://www.subirimagenes.com/otros-add1-7406511.html

編輯:

ViewModel類

public class AsignarEmpresa 
    { 
     public int EmpresaId { get; set; } 
     public string Nombre { get; set; } 
     public string cargo { get; set; } 
     public bool Assigned { get; set; } 
    } 

回答

0

看看你的發佈行爲和它的參數。這些名字非常重要。

你的複選框

<input type="checkbox" 
          name="selectedEmpresa" 
          value="@empresa.EmpresaId" 

工作正常怎麼一回事,因爲看看輸入其「selectedEmpresa」這是相同的名稱控制器動作定義的名稱。模型聯編程序在發佈的數據中查找此名稱,如果他找到它,他會從中創建對象。在你的情況下,他會嘗試解析數據到string []對象。

所以......首先你必須將動作定義改成類似的東西。

[HttpPost] 
     public ActionResult AgregarEmpresas(int? id, FormCollection formCollection, string[] selectedEmpresa,string [] empresaTextBox) 
     { 
} 

然後你必須改變生成的html。

@Html.TextBoxFor(model => empresa.cargo, new { style = "width: 150px;", name="empresaTextBox" }) 

與你應該得到的內部行動一些數據,這些變化。然而,你會得到一些奇怪的,因爲你有多個複選框文本框爲了告訴模型聯編程序,有多個元素,你必須準備包含索引號的輸入的特殊名稱。

看看這個例子

<input name="childs[0]"></input> 
<input name="childs[1]"></input> 

在這種情況下模型綁定將創建包含這些的2對象的陣列。

因此,最後你的代碼將看起來像這樣。

@using (Html.BeginForm("AgregarEmpresas", "Empleado")) 
{ 

    <fieldset> 
<div class="editor-field"> 
    <table> 
     <tr> 
      @{ 
       int cnt = 0; 
       int i=0; 
       List<ITCOrganigrama.ViewModel.AsignarEmpresa> empresas = ViewBag.Empresas; 

       foreach (var empresa in empresas) 
       { 
        if (cnt++ % 5 == 0) { 
         @: </tr> <tr> 
        } 
        @: <td> 
         <input type="checkbox" 
           name="selectedEmpresa[@i]" 
           value="@empresa.EmpresaId" 
           @(Html.Raw(empresa.Assigned ? "checked=\"checked\"" : "")) /> 
          @empresa.Nombre 
         <div> 
          @Html.LabelFor(model => empresa.cargo) 
          @Html.TextBoxFor(model => empresa.cargo, new { style = "width: 150px;" ,name=String.Format("empresaTextBox\[{0}\]",i) }) 
          @Html.ValidationMessageFor(model => empresa.cargo) 
         </div> 
        @:</td> 
       i++; 
       } 
       @: </tr> 

      } 
    </table> 
</div> 
<p> 
      <input type="submit" value="Agregar" /> 
     </p> 
</fieldset> 
} 

如果你會得到這個工作。然後我會嘗試創建一個類布爾值和一個字符串值。有了這個改變,你可以在類的數組上操作而不是使用兩個帶有字符串的數組。

+0

一個在我的行動中,我得到空empresaTextBox 我在我的viewmodel有一個類與布爾和字符串 – 2012-02-06 18:33:03

相關問題