2016-06-07 126 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