在HttpPost中使用模型数据和FormCollection

时间:2012-05-02 11:22:45

标签: asp.net-mvc-3

我在VIEW中有模型数据,还有'其他'数据,这不是模型的一部分。

当用户点击提交时,从VIEW中,我预计模型数据也会通过?

控制器中的我的HttpPost看起来像这样:

    [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);

但是,我在VIEW中还有其他数据,我想要POST到控制器,因此我添加了FormCollection。

在调试中,我看到RegisterModel为null,但FormCollection包含所有数据,包括模型和“其他”数据。我可以使用FormCollection,但是我需要获得很多“其他”数据,并循环(使用for语句)formCollection,并获取特定类型字段的记录数。基本上,我有checkBoxes,我需要循环,他们都被称为,code1,code2,code3 ... code24 ...我需要用“代码*”获取所有的,并将这些插入数据库。如果我只有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="code@(l_code)" value="@item.expertiseDesc" />
                        <b>@item.expertiseDesc</b>
                    </td> 
               }
               else if (l_count < 3)
               {    
                    <td class="tdcheckbox">
                        <input type="checkbox" name="code@(l_code)" value="@item.expertiseDesc" />
                        <b>@item.expertiseDesc</b>
                    </td>
               }
               else if (l_count == 3)
               {           
                    <td class="tdcheckbox">
                        <input type="checkbox" name="code@(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="code@(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>
}

这是我的控制器,HTTPPOST部分,我需要访问用户点击的CheckBox值

    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();

1 个答案:

答案 0 :(得分:0)

您可以通过

访问您的FormCollection
[HttpPost] 
public ActionResult RegisterSP(RegisterModel model) 
{ 
  var myForm = Request.Form;
  ....
}

不同的类,但基类NameValueCollection