【问题标题】:Using model data and FormCollection in a HttpPost在 HttpPost 中使用模型数据和 FormCollection
【发布时间】:2012-05-02 11:22:45
【问题描述】:

我在 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 中还有其他数据,我也想将其发布到控制器,因此我添加了 FormCollection。

在调试时,我看到 RegisterModel 为空,但 FormCollection 包含来自模型和“其他”数据的所有数据。我可以只使用 FormCollection,但是我需要获取很多“其他”数据,并循环(使用 for 语句)formCollection,并获取特定类型字段的记录数。基本上,我有复选框,我需要循环遍历它们,它们都被称为,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="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();

【问题讨论】:

  • 您的原始控制器方法是什么样的,以及您的观点。您能否提供问题的简化版本?
  • 好吧,现在,我只需要访问 FormCollection。目前,FormCollection 包含大约 50 个不同的字段,在这 50 个字段中,我需要访问 (24) 以下字段:code1、code2、code3 ..code24。我需要遍历这个列表并获取值。我可以使用通配符从 FormCollection 访问数据吗?最好的方法是什么?
  • 我认为您需要阅读模型绑定。如果您提供当前视图和原始控制器操作,则更容易提供帮助
  • 解决方案可以在这里找到..stackoverflow.com/q/4893147/1278124

标签: asp.net-mvc-3


【解决方案1】:

您可以通过

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

不同的类但相同的基类NameValueCollection

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多