您好我正在编写代码来更新数据库,但我面临一些问题,希望您可以指导我
所以我想通过我的mvc应用程序更新我数据库中的数据
以下控制器代码正常工作: -
[HttpGet]
[ActionName("Edit")]
public ActionResult Edit_Get(int id)
{
DealMasterBLL dealMasterBLL = new DealMasterBLL();
DealMasterInfo dealMasterInfo = dealMasterBLL.DealMasterInfo.Single(did => did.DealAutoID == Convert.ToString(id));
return View(dealMasterInfo);
}
[HttpPost]
[ActionName("Edit")]
public ActionResult Edit_Post(DealMasterInfo dealMasterInfo)
{
if (ModelState.IsValid)
{
DealMasterBLL dealMasterBLL = new DealMasterBLL();
// dealMasterInfo = dealMasterBLL.DealMasterInfo.Single(did => did.DealAutoID ==id);
// UpdateModel(dealMasterInfo);
dealMasterBLL.EditDealMaster(dealMasterInfo);
return RedirectToAction("Index");
}
return View();
}
但是当我在post方法中进行更改时
以下更改: -
[HttpGet]
[ActionName("Edit")]
public ActionResult Edit_Get(int id)
{
DealMasterBLL dealMasterBLL = new DealMasterBLL();
DealMasterInfo dealMasterInfo = dealMasterBLL.DealMasterInfo.Single(did => did.DealAutoID == Convert.ToString(id));
return View(dealMasterInfo);
}
[HttpPost]
[ActionName("Edit")]
public ActionResult Edit_Post(int id)
{
DealMasterBLL dealMasterBLL = new DealMasterBLL();
DealMasterInfo dealMasterInfo = dealMasterBLL.DealMasterInfo.Single(did => did.DealAutoID ==Convert.ToString(id));
UpdateModel(dealMasterInfo);
if (ModelState.IsValid)
{
dealMasterBLL.EditDealMaster(dealMasterInfo);
return RedirectToAction("Index");
}
return View();
}
代码不起作用 我试过调试,但它正确地通过所有路径没有任何错误
我正在添加以下所有其他必需代码,请回复
业务层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using SnackoEntities;
namespace BusinesssLayer
{
public class DealMasterBLL
{
public IEnumerable<DealMasterInfo> DealMasterInfo
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["DealMaster"].ConnectionString;
List<DealMasterInfo> dealMasterInfos = new List<DealMasterInfo>();
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("spGetDealMasterInfo", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
DealMasterInfo dealMasterInfo = new DealMasterInfo();
dealMasterInfo.DealAutoID = rdr["DealAutoID"].ToString();
dealMasterInfo.FK_RestoID = rdr["FK_RestoID"].ToString();
dealMasterInfo.FK_SnFoodID = rdr["FK_SnFoodID"].ToString();
dealMasterInfo.DealDisPerc = rdr["DealDisPerc"].ToString();
dealMasterInfo.FK_RewaID = rdr["FK_RewaID"].ToString();
dealMasterInfo.startDate = Convert.ToDateTime(rdr["startDate"]);
dealMasterInfo.endDate = Convert.ToDateTime(rdr["endDate"]);
dealMasterInfo.ValidOn = rdr["ValidOn"].ToString();
dealMasterInfo.UserTypes = rdr["UserTypes"].ToString();
dealMasterInfo.DealCode = rdr["DealCode"].ToString();
dealMasterInfo.DealPic = rdr["DealPic"].ToString();
dealMasterInfo.DealStatus = rdr["DealStatus"].ToString();
dealMasterInfo.CreatedOn = Convert.ToDateTime(rdr["CreatedOn"]);
dealMasterInfo.ModifiedOn = Convert.ToDateTime(rdr["ModifiedOn"]);
dealMasterInfo.CreatedBy = rdr["CreatedBy"].ToString();
dealMasterInfos.Add(dealMasterInfo);
}
}
return dealMasterInfos;
}
}
public void AddDealMaster(DealMasterInfo dealMasterInfo)
{
string connectionString = ConfigurationManager.ConnectionStrings["DealMaster"].ConnectionString;
try
{
using(SqlConnection con=new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("spAddDealMaster", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramDealAutoID = new SqlParameter();
paramDealAutoID.ParameterName = "@DealAutoID";
paramDealAutoID.Value = dealMasterInfo.DealAutoID;
cmd.Parameters.Add(paramDealAutoID);
SqlParameter paramFK_RestoID = new SqlParameter();
paramFK_RestoID.ParameterName = "@FK_RestoID";
paramFK_RestoID.Value = dealMasterInfo.FK_RestoID;
cmd.Parameters.Add(paramFK_RestoID);
SqlParameter paramFK_SnFoodID = new SqlParameter();
paramFK_SnFoodID.ParameterName = "@FK_SnFoodID";
paramFK_SnFoodID.Value = dealMasterInfo.FK_SnFoodID;
cmd.Parameters.Add(paramFK_SnFoodID);
SqlParameter paramFK_RoFoodID = new SqlParameter();
paramFK_RoFoodID.ParameterName = "@FK_RoFoodID";
paramFK_RoFoodID.Value = dealMasterInfo.FK_RoFoodID;
cmd.Parameters.Add(paramFK_RoFoodID);
SqlParameter paramDealDisPerc = new SqlParameter();
paramDealDisPerc.ParameterName = "@DealDiscPerc";
paramDealDisPerc.Value = dealMasterInfo.DealDisPerc;
cmd.Parameters.Add(paramDealDisPerc);
SqlParameter paramFK_RewaID = new SqlParameter();
paramFK_RewaID.ParameterName = "@FK_RewaID";
paramFK_RewaID.Value = dealMasterInfo.FK_RewaID;
cmd.Parameters.Add(paramFK_RewaID);
SqlParameter paramstartDate = new SqlParameter();
paramstartDate.ParameterName = "@startDate";
paramstartDate.Value = dealMasterInfo.startDate;
cmd.Parameters.Add(paramstartDate);
SqlParameter paramendDate = new SqlParameter();
paramendDate.ParameterName = "@endDate";
paramendDate.Value = dealMasterInfo.endDate;
cmd.Parameters.Add(paramendDate);
SqlParameter paramValidOn = new SqlParameter();
paramValidOn.ParameterName = "@ValidOn";
paramValidOn.Value = dealMasterInfo.ValidOn;
cmd.Parameters.Add(paramValidOn);
SqlParameter paramUserTypes = new SqlParameter();
paramUserTypes.ParameterName = "@UserTypes";
paramUserTypes.Value = dealMasterInfo.UserTypes;
cmd.Parameters.Add(paramUserTypes);
SqlParameter paramDealCode = new SqlParameter();
paramDealCode.ParameterName = "@DealCode";
paramDealCode.Value = dealMasterInfo.DealCode;
cmd.Parameters.Add(paramDealCode);
SqlParameter paramDealPic = new SqlParameter();
paramDealPic.ParameterName = "@DealPic";
paramDealPic.Value = dealMasterInfo.DealPic;
cmd.Parameters.Add(paramDealPic);
SqlParameter paramDealStatus = new SqlParameter();
paramDealStatus.ParameterName = "@DealStatus";
paramDealStatus.Value = dealMasterInfo.DealStatus;
cmd.Parameters.Add(paramDealStatus);
SqlParameter paramCreatedOn = new SqlParameter();
paramCreatedOn.ParameterName = "@CreatedOn";
paramCreatedOn.Value = dealMasterInfo.CreatedOn;
cmd.Parameters.Add(paramCreatedOn);
SqlParameter paramModifiedOn = new SqlParameter();
paramModifiedOn.ParameterName = "@ModifiedOn";
paramModifiedOn.Value = dealMasterInfo.ModifiedOn;
cmd.Parameters.Add(paramModifiedOn);
SqlParameter paramCreatedBy = new SqlParameter();
paramCreatedBy.ParameterName = "@CreatedBy";
paramCreatedBy.Value = dealMasterInfo.CreatedBy;
cmd.Parameters.Add(paramCreatedBy);
con.Open();
cmd.ExecuteNonQuery();
}
}
catch(Exception ex){
Console.WriteLine("SQL Error" + ex.Message.ToString());
}
}
public void EditDealMaster(DealMasterInfo dealMasterInfo)
{
string connectionString = ConfigurationManager.ConnectionStrings["DealMaster"].ConnectionString;
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("spEditDealMaster", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramDealAutoID = new SqlParameter();
paramDealAutoID.ParameterName = "@DealAutoID";
paramDealAutoID.Value = dealMasterInfo.DealAutoID;
cmd.Parameters.Add(paramDealAutoID);
SqlParameter paramFK_RestoID = new SqlParameter();
paramFK_RestoID.ParameterName = "@FK_RestoID";
paramFK_RestoID.Value = dealMasterInfo.FK_RestoID;
cmd.Parameters.Add(paramFK_RestoID);
SqlParameter paramFK_SnFoodID = new SqlParameter();
paramFK_SnFoodID.ParameterName = "@FK_SnFoodID";
paramFK_SnFoodID.Value = dealMasterInfo.FK_SnFoodID;
cmd.Parameters.Add(paramFK_SnFoodID);
SqlParameter paramFK_RoFoodID = new SqlParameter();
paramFK_RoFoodID.ParameterName = "@FK_RoFoodID";
paramFK_RoFoodID.Value = dealMasterInfo.FK_RoFoodID;
cmd.Parameters.Add(paramFK_RoFoodID);
SqlParameter paramDealDisPerc = new SqlParameter();
paramDealDisPerc.ParameterName = "@DealDiscPerc";
paramDealDisPerc.Value = dealMasterInfo.DealDisPerc;
cmd.Parameters.Add(paramDealDisPerc);
SqlParameter paramFK_RewaID = new SqlParameter();
paramFK_RewaID.ParameterName = "@FK_RewaID";
paramFK_RewaID.Value = dealMasterInfo.FK_RewaID;
cmd.Parameters.Add(paramFK_RewaID);
SqlParameter paramstartDate = new SqlParameter();
paramstartDate.ParameterName = "@startDate";
paramstartDate.Value = dealMasterInfo.startDate;
cmd.Parameters.Add(paramstartDate);
SqlParameter paramendDate = new SqlParameter();
paramendDate.ParameterName = "@endDate";
paramendDate.Value = dealMasterInfo.endDate;
cmd.Parameters.Add(paramendDate);
SqlParameter paramValidOn = new SqlParameter();
paramValidOn.ParameterName = "@ValidOn";
paramValidOn.Value = dealMasterInfo.ValidOn;
cmd.Parameters.Add(paramValidOn);
SqlParameter paramUserTypes = new SqlParameter();
paramUserTypes.ParameterName = "@UserTypes";
paramUserTypes.Value = dealMasterInfo.UserTypes;
cmd.Parameters.Add(paramUserTypes);
SqlParameter paramDealCode = new SqlParameter();
paramDealCode.ParameterName = "@DealCode";
paramDealCode.Value = dealMasterInfo.DealCode;
cmd.Parameters.Add(paramDealCode);
SqlParameter paramDealPic = new SqlParameter();
paramDealPic.ParameterName = "@DealPic";
paramDealPic.Value = dealMasterInfo.DealPic;
cmd.Parameters.Add(paramDealPic);
SqlParameter paramDealStatus = new SqlParameter();
paramDealStatus.ParameterName = "@DealStatus";
paramDealStatus.Value = dealMasterInfo.DealStatus;
cmd.Parameters.Add(paramDealStatus);
SqlParameter paramCreatedOn = new SqlParameter();
paramCreatedOn.ParameterName = "@CreatedOn";
paramCreatedOn.Value = dealMasterInfo.CreatedOn;
cmd.Parameters.Add(paramCreatedOn);
SqlParameter paramModifiedOn = new SqlParameter();
paramModifiedOn.ParameterName = "@ModifiedOn";
paramModifiedOn.Value = dealMasterInfo.ModifiedOn;
cmd.Parameters.Add(paramModifiedOn);
SqlParameter paramCreatedBy = new SqlParameter();
paramCreatedBy.ParameterName = "@CreatedBy";
paramCreatedBy.Value = dealMasterInfo.CreatedBy;
cmd.Parameters.Add(paramCreatedBy);
con.Open();
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Console.WriteLine("SQL Error" + ex.Message.ToString());
}
}
}
}
实体
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SnackoEntities
{
public class DealMasterInfo
{
public string DealAutoID { get; set; }
public string FK_RestoID { get; set; }
public string FK_SnFoodID { get; set; }
public string FK_RoFoodID { get; set; }
public string DealDisPerc { get; set; }
public string FK_RewaID { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public string ValidOn { get; set; }
public string UserTypes { get; set; }
public string DealCode { get; set; }
public string DealPic { get; set; }
public string DealStatus { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime ModifiedOn { get; set; }
public string CreatedBy { get; set; }
}
}
HTML for edit
@model SnackoEntities.DealMasterInfo
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>DealMasterInfo</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.DealAutoID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.DealAutoID, new { htmlAttributes = new { @class = "form-control" } })
@Html.HiddenFor(model => model.DealAutoID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DealAutoID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FK_RestoID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FK_RestoID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FK_RestoID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FK_SnFoodID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FK_SnFoodID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FK_SnFoodID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FK_RoFoodID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FK_RoFoodID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FK_RoFoodID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DealDisPerc, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DealDisPerc, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DealDisPerc, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FK_RewaID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FK_RewaID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FK_RewaID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.startDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.startDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.startDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.endDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.endDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.endDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ValidOn, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ValidOn, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ValidOn, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserTypes, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserTypes, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserTypes, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DealCode, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DealCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DealCode, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DealPic, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DealPic, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DealPic, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DealStatus, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DealStatus, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DealStatus, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CreatedOn, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CreatedOn, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CreatedOn, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ModifiedOn, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ModifiedOn, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ModifiedOn, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CreatedBy, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CreatedBy, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CreatedBy, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
存储过程
create procedure spEditDealMaster
@DealAutoID varchar(255),
@FK_RestoID varchar(255),
@FK_SnFoodID varchar(255),
@FK_RoFoodID varchar(255),
@DealDiscPerc varchar(255),
@FK_RewaID varchar(255),
@startDate smalldatetime,
@endDate smalldatetime,
@ValidOn varchar(255),
@UserTypes varchar(255),
@DealCode varchar(255),
@DealPic varchar(255),
@DealStatus varchar(255),
@CreatedOn smalldatetime,
@ModifiedOn smalldatetime,
@CreatedBy varchar(255)
as
Begin
Update DealMaster set DealAutoID=@DealAutoID,FK_RestoID=@FK_RestoID,FK_SnFoodID=@FK_SnFoodID,FK_RoFoodID=@FK_RoFoodID,DealDisPerc =@DealDiscPerc,FK_RewaID=@FK_RewaID,startDate=@startDate,endDate=@endDate,ValidOn=@ValidOn,UserTypes=@UserTypes,DealCode=@DealCode,DealPic=@DealPic,DealStatus=@DealStatus,CreatedOn=@CreatedOn,ModifiedOn=@ModifiedOn,CreatedBy=@CreatedBy
where DealAutoID=@DealAutoID
End
请帮忙.. 评论我是否遗漏了什么