2

I have model data in a VIEW, and 'other' data as well, which is not part of a model.

When the user clicks on submit, from the VIEW, i am expecting the model data to also go through?

My HttpPost in the controller looks as such:

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

However, i also have other data in the VIEW which i would like to POST as well to the controller, hence i have added FormCollection.

When in debug, i see the the RegisterModel, is null, but the FormCollection has ALL the data, both from the model and the 'other' data as well. I can just use the FormCollection, however there are quite alot of 'other' data which i need to get, and loop through (using for statements) the formCollection, and get the number of records for a particular type of field. Basically, i have checkBoxes, which i need to loop through, and they are all called, code1, code2,code3 ... code24 ... I need to get all the ones with "code*" and insert these into the Database. If i just have the FormCollection having the checkboxes i will be fine, but i cant. Is there a way for me to get the FormCollection["code*"] for example, so using a wildcard to get all the ones (checkboxes) that i need ? Or am i doing all this the wrong way

Here is my 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>
}

And here is my controller, the HTTPPOST part, where i need to access the CheckBox values that were clicked by the user

    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();
Naren Gokal
  • 117
  • 8
  • 21
  • What does your originating controller method look like, and your view. Could you provide a simplified version of your problem? – Tomas Jansson May 02 '12 at 11:45
  • Well, now, i just need to access FormCollection. At the moment, FormCollection contains about 50 different fields, and out of this 50,i need to access the (24) following fields : code1, code2, code3 .. code24. I need to loop through this list and get the values. Can i use a wildcard for accessing data from FormCollection? what is the best way to do this ? – Naren Gokal May 02 '12 at 12:31
  • I think you need to read up on model binding. If you provide your current view and the originating controller action it is much easier to help – Tomas Jansson May 02 '12 at 12:36
  • solution can be found here .. http://stackoverflow.com/q/4893147/1278124 – Naren Gokal May 02 '12 at 13:57

1 Answers1

0

you can access to your FormCollection via

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

different classes but same base class NameValueCollection

seddik
  • 639
  • 1
  • 8
  • 20