ASP MVC 5とADO.netを開発しています。 は、ストアドプロシージャを経由して(更新)利用者(クライアント)のproperitiesを編集しようと、私はいつも同上のパラメータを使用して、SQL例外が発生しました:ASP MVCでの更新による一意のキーの違反を回避する方法
私はそれを削除すると、例外がある:
プロシージャーまたは関数 'UpdateClientsInfo'は、指定されていないパラメーター '@CltId'を要求します。
そして、私はCltIdを追加するとき、それは私が表示されます:UNIQUE KEY制約 'UQ__Client__A9D1053400F8EEAC' の
違反を。オブジェクト 'dbo.Client'に重複キーを挿入できません。 。重複するキー値が([email protected])で」多くのフォーラムでは、検索に基づか
、私はこの行を追加しようとしましたが、それはうまくいきませんでした:
cmd.CommandType = CommandType.StoredProcedure;
ここでは私のコントローラです:
[HttpPost]
public ActionResult Edit(int id, Client cmodel)
{
try
{
ClientManagement cdb = new ClientManagement();
if (cdb.UpdateDetails(cmodel))
{
ViewBag.Message = "Client Details Edited Successfully";
ModelState.Clear();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
、ここではClientManagementクラスです:
public bool UpdateDetails(Client cmodel)
{
try
{
connection();
SqlCommand cmd = new SqlCommand("UpdateClientsInfo", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CltId", cmodel.ClientId);
cmd.Parameters.AddWithValue("@FirstName", cmodel.FirstName);
cmd.Parameters.AddWithValue("@LastName", cmodel.LastName);
cmd.Parameters.AddWithValue("@Email", cmodel.Email);
cmd.Parameters.AddWithValue("@Phone", cmodel.Phone);
cmd.Parameters.AddWithValue("@Address", cmodel.Address);
cmd.Parameters.AddWithValue("@Password", cmodel.Password);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i >= 1)
return true;
else
return false;
}
catch (SqlException sqlexc)
{
foreach (SqlError error in sqlexc.Errors)
{
string msg = string.Format("{0}: {1}", error.Number, error.Message);
}
return false;
}
}
モデルクライアント:
CREATE procedure [dbo].[UpdateClientsInfo]
(
@CltId int,
@FirstName nvarchar (50),
@LastName nvarchar (50),
@Email nvarchar (50),
@Phone nvarchar(50),
@Address nvarchar(50),
@Password nvarchar (max)
)
as
begin
Update Client
set
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected]
End
ビューの編集からのコードの抜粋:
public class Client
{
[Display(Name = "Id")]
[Range(0, 15)]
public int ClientId { get; set; }
[Display(Name = "First Name")]
[Required(ErrorMessage = "First name is required.")]
[StringLength(30)]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
[Required(ErrorMessage = "Last name is required.")]
[StringLength(30)]
public string LastName { get; set; }
[Display(Name = "Email")]
[Required(ErrorMessage = "Email is required.")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
[DataType(DataType.EmailAddress)]
[StringLength(30)]
public string Email { get; set; }
[Display(Name = "Phone")]
[DataType(DataType.PhoneNumber)]
[Required(ErrorMessage = "Phone is required.")]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Entered phone format is not valid.")]
public String Phone { get; set; }
[Display(Name = "Address")]
[Required(ErrorMessage = "Address is required.")]
[StringLength(30)]
public string Address { get; set; }
[Display(Name = "Password")]
[DataType(DataType.Password)]
[Required(ErrorMessage = "Password is required.")]
public string Password { get; set; }
}
ストアドプロシージャのためのDBスキーマ
body style="background-image: url('/template/web/images/fd.jpg'); background-repeat: no-repeat; background-size: cover;">
@using (Html.BeginForm("Edit", "Client", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal" runat="server">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ClientId)
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
上記の手順を完全に実装してもよろしいですか? – borkovski
@ borkovskiあなたはストアドプロシージャを意味します、はい! – Exact