2016-06-07 155 views
-1

我試圖創建一個將字符串存儲到數據庫的方法。但是,我的參數似乎並不存在於當前的情況下。我確實有system.IO的方式聲明,但我仍然無法讓代碼識別參數。我怎樣才能解決這個問題?提前致謝。保存到數據庫(mvc)

[HttpPost] 
    public void SaveAccount(string accountId, string AccountName, string Address, string City, string State, string ZipCode, string PhoneNumber, string IsActive) 
    public PartialViewResult SaveAccount(FormCollection form) 
    { 
     string accountid = form["SaveAccount"].ToString(); 

     using (OdbcConnection _conn = new OdbcConnection("FILEDSN=c:\\datasources\\RxCard.dsn")) 
     using (OdbcCommand cmd1 = new OdbcCommand()) 
     { 
      cmd1.Connection = _conn; 
      cmd1.CommandText = "{call web.SaveAccount(?,?,?,?,?,?,?,?)}"; 
      cmd1.Parameters.AddWithValue("@AccountID", accountId); 
      cmd1.Parameters.AddWithValue("@AccountName", AccountName); 
      cmd1.Parameters.AddWithValue("@Address", Address); 
      cmd1.Parameters.AddWithValue("@City", City); 
      cmd1.Parameters.AddWithValue("@State", State); 
      cmd1.Parameters.AddWithValue("@ZipCode", ZipCode); 
      cmd1.Parameters.AddWithValue("@PhoneNumber", PhoneNumber); 
      cmd1.Parameters.AddWithValue("@IsActive", IsActive); // need to handle conversion from string to boolean on stored proc 

      cmd1.Parameters.AddWithValue("@AccountName", form["AccountName"].ToString()); 
      cmd1.Parameters.AddWithValue("@Address", form["Address"].ToString()); 
      cmd1.Parameters.AddWithValue("@City", form["City"].ToString()); 
      cmd1.Parameters.AddWithValue("@State", form["State"].ToString()); 
      cmd1.Parameters.AddWithValue("@ZipCode", form["ZipCode"].ToString()); 
      cmd1.Parameters.AddWithValue("@PhoneNumber", form["PhoneNumber"].ToString()); 
      cmd1.Parameters.AddWithValue("@IsActive", form["IsActive"].ToString()); // need to handle conversion from string to boolean on stored proc 
      cmd1.CommandType = CommandType.StoredProcedure; 
      _conn.Open(); 
      cmd1.ExecuteNonQuery(); 
      _conn.Close(); 
     } 

形式:

@using (Html.BeginForm("SaveAccount", "RxCard", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
       <div id="Edit"> 
        <label id="lblAccountName">Account Name</label> 
        <input style="margin: 5px; " type=text name="txtAccountName" id="txtAccountName" value="@Model.Pharmacy.AccountName" /> 
        <label id="lblAddress">Address</label> 
        <input style="margin: 5px; " type=text name="txtAddress" id="txtAddress" value="@Model.Pharmacy.Address" /> 
        <label id="lblCity">City</label> 
        <input style="margin: 5px; " type=text name="txtCity" id="txtCity" value="@Model.Pharmacy.City" /> 
        <label id="lblState">State</label> 
        <input style="margin: 5px; " type=text name="txtState" id="txtState" value="@Model.Pharmacy.State" /> 
        <label id="lblZip">Zip</label> 
        <input style="margin: 5px; " type=text name="txtZip" id="txtZip" value="@Model.Pharmacy.ZipCode" /> 
        <label id="lblPhoneNumber">Phone Number</label> 
        <input style="margin: 5px; " type=text name="txtPhoneNumber" id="txtPhoneNumber" value="@Model.Pharmacy.PhoneNumber" /> 
       </div> 

       <div id="Add"> 

       </div> 

       <div id="ChkBox"> 
        <input id="chkIsActive" style="margin: 5px; " type="checkbox" name="chkIsActive" onchange='Areyousure(this)' value="@Model.Pharmacy.IsActive" @(Model.Pharmacy.IsActive =="True" ? "checked=\"checked\"" : "") /> Is Active 
       </div> 

       <br/> 
       <button type="submit" name="EditAccount" id="EditAccount" value="Edit">Edit</button> 
       <button type="submit" name="SaveAccount" id="SaveAccount" value="@Model.Pharmacy.AccountID">Save</button> 
       // <button type="submit" name="AddAccount" id="AddAccount" value="Add">Add</button> 

       } 
+0

你發佈到這個動作的表單是什麼樣的?另外,如果你正在傳入你的參數,你可以考慮使用合適的解析類型在傳遞它們之前轉換這些值(例如'Convert.ToBoolean(form [「IsActive」])'等) –

+0

對不起,表單包括在上面。謝謝! – julianc

回答

0

name屬性將是當<form>被張貼處理映射鍵/值的屬性,所以你需要調整這些匹配是什麼出現在您的形式(即的,而不是txtAccountNameAccountName):

cmd1.Parameters.AddWithValue("@AccountName",form["txtAccountName"]); 
cmd1.Parameters.AddWithValue("@Address",form["txtAddress"]); 
// Other string parameters omitted for brevity 
cmd1.Parameters.AddWithValue("@IsActive", Convert.ToBoolean(form["chkIsActive"])); 

值得一提的是,MVC不會自動盟友堅持爲你的價值觀,所以將出現在你的控制器行動中的唯一值將是那些提前明確存儲的數據(類似於TempData,數據庫或會話)以及發佈在你的形成自己。

+0

感謝您的幫助。我也收到錯誤:「字符串IsActive)」。 Visual studio建議添加一個;但那不能解決它。 – julianc

+0

這是指哪一行? –

+0

改爲「public void SaveAccount .... string IsActive)」 – julianc