我在 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*"] 例如,所以使用通配符来获得我需要的所有(复选框)?还是我做错了这一切
这是我的视图(),
@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();