2012-05-02 9 views
2

モデルデータはVIEW内にあり、「その他」のデータもモデルの一部ではありません。HttpPostでのモデルデータとFormCollectionの使用

ユーザーがVIEWからサブミットをクリックすると、モデルデータも通過すると予想されますか?

マイHttpPostコントローラーでそのようになります。しかし、私はまた、私はコントローラにも同様にPOSTしたいVIEW内の他のデータを持っている

[HttpPost] 
    public ActionResult RegisterSP(RegisterModel model, FormCollection collection) 
    { 
     System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); 

     // Create Bytes using userName and Email to calculate CRC32 
     String s_user_id = SessionBag.Current.UserName + SessionBag.Current.Email; 
     Byte[] bytes_user_id = encoding.GetBytes(s_user_id); 
     UInt32 user_Id = Crc32.Compute(bytes_user_id); 

、それゆえ私はFormCollectionを追加しました。

デバッグ時には、RegisterModelがnullですが、FormCollectionにはモデルと 'その他の'データの両方のデータがすべて含まれています。私はFormCollectionを使用することができますが、かなり必要な「その他の」データがあり、formCollectionをループして(for文を使用して)、特定のタイプのフィールドのレコード数を取得します。基本的には、私はループする必要があるcheckBoxesを持っています、そして、それらはすべてcode1、code2、code3、... code24 ... "code *"を持つすべてのものを取得し、これらをデータベースに挿入する必要があります。私はちょうどチェックボックスを持っているFormCollectionを持っている場合、私は上手くいくだろうが、私は傾ける。たとえばFormCollection ["code *"]を取得する方法はありますか?ワイルドカードを使用して必要なすべてのもの(チェックボックス)を取得する方法はありますか?または私は間違った方法ここ

は私のVIEWである()このすべてをやって、ここ

@model mvc1.Models.RegisterModel 

@{ 
    ViewBag.Title = "RegisterSP"; 
} 

<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> 

@section LoginSignupLink { 
    @Html.Partial("_LogOnPartial") 
    @Html.Partial("_SignUpPartial") 
} 

@{ 
    Layout = "~/Views/Shared/_InitLayout.cshtml"; 
} 


@using (Html.BeginForm()) 
{ 
<h2>Create a New Account (Page 2)</h2> 
<p> 
    Use the form below to continue creating a new account. 
</p> 

    <div> 
    <fieldset style = "width: 840px; margin: 0px auto;"> 
    <legend>Account Information</legend> 
     <br /> 
     <b><font color="#000000" size="4">Address Details</font></b> 
     <br /> 
     <br /> 
     <table class="tablelogon"> 
      <tr> 
      <th class="thlogon">Company</th> 
      <td class="tdlogon">@Html.TextBoxFor(m => m.userNameCompany, new { style = "font-family:Verdana;font-size:13px;color:#000000" }) 
           @Html.ValidationMessageFor(m => m.userNameCompany) 
      </td> 
      <th class="thlogon">Address-Building Name</th> 
      <td class="tdlogon">@Html.TextBoxFor(m => m.buildingName, new { style = "font-family:Verdana;font-size:13px;color:#000000" }) 
           @Html.ValidationMessageFor(m => m.buildingName) 
      </td> 
      </tr> 

      <tr> 
      <th class="thlogon">Address-Building Number</th> 
      <td class="tdlogon">@Html.TextBoxFor(m => m.buildingNumber, new { style = "font-family:Verdana;font-size:13px;color:#000000" }) 
           @Html.ValidationMessageFor(m => m.buildingNumber) 
      </td> 
      <th class="thlogon">Address-Street Number</th> 
      <td class="tdlogon">@Html.TextBoxFor(m => m.streetNumber, new { style = "font-family:Verdana;font-size:13px;color:#000000" }) 
           @Html.ValidationMessageFor(m => m.streetNumber) 
      </td> 
      </tr> 

      <tr> 
      <th class="thlogon">Address-Street Name</th> 
      <td class="tdlogon">@Html.TextBoxFor(m => m.streetName, new { style = "font-family:Verdana;font-size:13px;color:#000000" }) 
           @Html.ValidationMessageFor(m => m.streetName) 
      </td> 
      <th class="thlogon">Address-Area</th> 
      <td class="tdlogon"> @Html.TextBoxFor(m => m.area, new { style = "font-family:Verdana;font-size:13px;color:#000000" }) 
           @Html.ValidationMessageFor(m => m.area) 
      </td> 
      </tr> 

      <tr> 
      <th class="thlogon">Address-City</th> 
      <td class="tdlogon">@Html.TextBoxFor(m => m.city, new { style = "font-family:Verdana;font-size:13px;color:#000000" }) 
           @Html.ValidationMessageFor(m => m.city) 
      </td> 
      <th class="thlogon">Address-Country</th> 
      <td class="tdlogon">@Html.TextBoxFor(m => m.country, new { style = "font-family:Verdana;font-size:13px;color:#000000" }) 
           @Html.ValidationMessageFor(m => m.country) 
      </td> 
      </tr> 

      <tr> 
      <th class="thlogon">Postal Code</th> 
      <td class="tdlogon">@Html.TextBoxFor(m => m.postalCode, new { style = "font-family:Verdana;font-size:13px;color:#000000" }) 
           @Html.ValidationMessageFor(m => m.postalCode) 
      </td> 
      </tr> 
     </table> 

     @{ int l_count = 0; 
      int l_code = 0; 
      string l_otherExpertise; 
     } 

     <br /> 
     <hr /> 
     <b><font color="#000000" size="4">Expertise</font></b> 
     <br /> 
     <br /> 
     <table class="tablecheckbox"> 
     @foreach (var item in ViewBag.Expertise) 
     {    
       l_count = l_count + 1;    
       l_code = l_code + 1; 

       if (l_count == 1) 
       {               
        <td class="tdcheckbox"> 
         <input type="checkbox" name="[email protected](l_code)" value="@item.expertiseDesc" /> 
         <b>@item.expertiseDesc</b> 
        </td> 
       } 
       else if (l_count < 3) 
       {  
        <td class="tdcheckbox"> 
         <input type="checkbox" name="[email protected](l_code)" value="@item.expertiseDesc" /> 
         <b>@item.expertiseDesc</b> 
        </td> 
       } 
       else if (l_count == 3) 
       {   
        <td class="tdcheckbox"> 
         <input type="checkbox" name="[email protected](l_code)" value="@item.expertiseDesc" /> 
         <b>@item.expertiseDesc</b> 
        </td>                
       } 
       else if (l_count % 2 == 0 && l_count == 4) 
       {   
        <td class="tdcheckbox"> 
         <input type="checkbox" name="[email protected](l_code)" value="@item.expertiseDesc" /> 
         <b>@item.expertiseDesc</b> 

        @if (@item.expertiseDesc == "Other") 
        { 
         @Html.TextBox("l_otherExpertise","") 
         <br /><font color="red">Comma separate specific expertise</font> 
        } 
        </td> 

        @:<tr></tr> 
        l_count = 0; 
       } 
     } 
     </table>   

     <br /> 
     <hr /> 
     <b><font color="#000000" size="4">Skills</font></b> 
     <br /> 
     <br /> 
     @Html.TextAreaFor(m => m.skills, new { style = "font-family:Verdana;font-size:13px; height: 50px; width:680px; overflow: scroll" }) 

     <br /> 
     <br /> 
     <hr /> 
     <b><font color="#000000" size="4">About</font></b> 
     <br /> 
     <br /> 
     @Html.TextAreaFor(m => m.about, new { style = "font-family:Verdana;font-size:13px; height: 50px; width:680px; overflow: scroll" }) 
     <p> 
     <input type="submit" value="SignUp"/> 
     </p> 
    </fieldset> 
    </div> 
} 

そして、私のコントローラ、iをクリックし、チェックボックスの値にアクセスする必要があるHTTPPOSTの一部でありますユーザー

public ActionResult RegisterSP(RegisterModel model, FormCollection collection) 
    { 
     System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); 

     // Create Bytes using userName and Email to calculate CRC32 
     String s_user_id = SessionBag.Current.UserName + SessionBag.Current.Email; 
     Byte[] bytes_user_id = encoding.GetBytes(s_user_id); 
     UInt32 user_Id = Crc32.Compute(bytes_user_id); 

     DBController dbcontroller = new DBController(); 
+0

元のコントローラーメソッドはどのように見えますか?あなたは問題の簡略版を提供できますか? –

+0

さて、今、私はFormCollectionにアクセスする必要があります。現時点でFormCollectionには約50のフィールドがあり、このうち50フィールドのうち、code1、code2、code3 .. code24のフィールドにアクセスする必要があります。このリストをループして値を取得する必要があります。 FormCollectionからデータにアクセスするためにワイルドカードを使用できますか?これを行う最善の方法は何ですか? –

+0

モデルバインディングを読んでみる必要があると思います。あなたが現在のビューと元のコントローラーのアクションを提供するなら、もっと簡単に –

答えて

0

あなたは

[HttpPost] 
public ActionResult RegisterSP(RegisterModel model) 
{ 
    var myForm = Request.Form; 
    .... 
} 

異なるCLAを経由してあなたのFormCollectionにアクセスすることができますssesでも同じ基本クラスNameValueCollection

関連する問題