我試圖創建一個將字符串存儲到數據庫的方法。但是,我的參數似乎並不存在於當前的情況下。我確實有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>
}
你發佈到這個動作的表單是什麼樣的?另外,如果你正在傳入你的參數,你可以考慮使用合適的解析類型在傳遞它們之前轉換這些值(例如'Convert.ToBoolean(form [「IsActive」])'等) –
對不起,表單包括在上面。謝謝! – julianc