CheckBox访问MVC3中的数据库

时间:2012-04-03 08:44:24

标签: asp.net-mvc-3

我有一个名为专业知识的表,其中包含“代码”和“描述”字段。此表中大约有20行。

我想要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();
            }

在我的视图中我有这个:      @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()中获取RegisterSPModel数据字段,即使我在VIEW的开头指定了它。

1 个答案:

答案 0 :(得分:0)

我猜您有以下型号:

@model IEnumerable<YourProject.Models.Expertise>

您需要做的就是将表格放在@html.beginForm中,并为每一行添加checkbox。并设置checkBox to "code"

的值

示例:

 @using (Html.BeginForm())
 {
     @foreach (var x in Model)
        {        
          <div>
               <table>     
                  <tr>
                    <td>
                      <input type="checkbox" name="codes" value="@x.code"  /> 
                    </td>
                    ... // rest of your table

然后在你的控制器中为页面添加一个post方法:

 [HttpPost, ActionName("Index")]
 public ActionResult IndexPost(List<CTypeofCode>codes)
 {
   // codes is going to be the list of what the user selected.
   // now you are ready to do what ever you need to do with the users selection
 }