2012-04-03 2 views
0

"코드"및 "설명"필드가 포함 된 전문 기술이라는 테이블이 있습니다.이 테이블에는 약 20 개의 행이 있습니다.CheckBox MVC3에서 데이터베이스 액세스

전문가 표의 설명이있는 체크 박스를 원하는대로 선택하여 버튼을 제출하면 전문 지식 테이블에서 '코드'를 가져 와서 게시해야합니다. 데이터 베이스.

어떻게하면됩니까? 누군가가 나에게 내 모델에서

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()에서 RegisterSPModel 데이터 필드를 가져올 수 없습니다. VIEW()의 시작 부분에 지정된 경우에도 마찬가지입니다.

답변

0

내가 추측하고 다음과 같은 모델을 가지고 :

@model IEnumerable<YourProject.Models.Expertise> 

당신이 @html.beginForm에 테이블을 넣어 할 각 행에 checkbox를 추가하는 데 필요한 모든.

@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 
} 
checkBox to "code"

예를 들어 값을 설정