0
我有一個名爲expertise的表,其中包含「code」和「description」字段。此表中有大約20行。CheckBox在MVC3中訪問數據庫
我想要20個複選框有專家表中的描述,以便用戶可以檢查儘可能多的複選框,一旦提交按鈕,它必須從專業知識表中取出「代碼」並將其發佈到數據庫。
我該怎麼做?能有人給我一步步的方式MVC3
在我的模型要做到這一點,我有以下幾點:
public class RegisterModel
{
[StringLength(20, ErrorMessage = "{0} must be at least {2} characters long and a maximum of {1} characters with no spaces.", MinimumLength = 6)]
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email { get; set; }
[Required]
[StringLength(20, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Display(Name = "UserPhone")]
public string userPhone { get; set; }
public class ExpertiseModel
{
public string expertiseCode { get; set; }
public string expertiseDesc { get; set; }
}
public class UserExpertiseModel
{
public string expertiseCode { get; set; }
public string otherExpertise { get; set; }
}
public class RegisterSPModel
{
UserExpertiseModel userexpertisemodel;
RegisterModel registermodel;
public RegisterSPModel()
{
userexpertisemodel = new UserExpertiseModel();
registermodel = new RegisterModel();
}
}
在我的控制器,我有:
// GET: /Account/RegisterSP
public ActionResult RegisterSP()
{
DBController dbcontroller = new DBController();
if (dbcontroller.DBConnection())
{
MySqlCommand command = new MySqlCommand("view_all_expertise;", dbcontroller.conn);
command.CommandType = System.Data.CommandType.StoredProcedure;
command.Parameters.Add(new MySqlParameter("expertiseCode", MySqlDbType.String));
command.Parameters["@expertiseCode"].Direction = System.Data.ParameterDirection.Output;
command.Parameters.Add(new MySqlParameter("expertiseDesc", MySqlDbType.String));
command.Parameters["@expertiseDesc"].Direction = System.Data.ParameterDirection.Output;
try
{
MySqlDataReader rdr = command.ExecuteReader();
var expmodel = new ExpertiseModel();
while (rdr.Read())
{
expmodel.expertiseCode = rdr["expertiseCode"].ToString();
expmodel.expertiseDesc = rdr["expertiseDesc"].ToString();
}
ViewBag.Expertise = expmodel;
return View();
}
在我VIEW我有這樣的: @model mvc1.Models.RegisterSPModel
@{
ViewBag.Title = "RegisterSP";
}
@using (Html.BeginForm()) {
<p>
Use the form below to continue creating a new account.
</p>
<div>
<fieldset style = "width: 840px; margin: 0px auto;">
<legend>Account Information - Service Provider</legend>
<br />
@foreach (var item in ViewBag.Expertise)
{
<div>
<table>
<tr>
<td>
<input type="checkbox" name="codes" value="@item.Expertise.expertiseDesc" />
</td>
</tr>
</table>
</div>
}
這不工作..即使我在VIEW的開頭指定了它,我也無法獲得VIEW()中的RegisterSPModel數據字段。