2012-04-03 25 views
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数据字段。

回答

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 

,然后在你的控制器中添加方法后的页面:

[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 
}