我还是MVC的新手。现在在我的控制器中我想执行基本的CRUD,如创建,编辑,详细信息。
我使用MySql作为数据库。
当我在调试模式下运行程序时,它没有显示错误。但是,在创建页面中,当我单击“提交”按钮时,输入的新数据不会在索引中更新。我想知道为什么并需要帮助。
你能告诉我在创作和编辑的哪个地方我做错了吗?提前谢谢。
控制器:
//GET UserActivity/Details/5
public ActionResult Details(int id)
{
string sqlQuery = @"
SELECT ua.Id
,ua.CreatedBy
,ua.CreatedOn
,ua.ModifiedBy
,ua.ModifiedOn
,ua.ContactId
,ua.EntityName
,ua.EntityId
,ua.StatusCode
,ua.StateCode
,ua.ActivityType
,ua.ActivityStatus
,ua.DueDate
,ua.ActualEndDate
,ua.MasqueradeOn
,ua.MasqueradeBy
FROM UserActivity ua
INNER JOIN Contact c ON c.Id = ua.ContactId
WHERE ua.id = @UserActivityId
";
UserActivityModels userActivity = null;
using (IDbConnection db = new MySqlConnection
(ConfigurationManager.ConnectionStrings["CRMPORTALSQLCONN"].
ConnectionString))
{
userActivity = (UserActivityModels)db.Query<UserActivityModels>(sqlQuery
, new
{
@UserActivityId = id
}).FirstOrDefault();
}
return View(userActivity);
}
/* // POST: /UserActivity/Details
[HttpPost]
public ActionResult Details(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Details");
}
catch
{
return View();
}
}
*/
//----------------------------------------------------------------------------------------------------//
//GET /UserActivity/Create
public ActionResult Create()
{
return View();
}
//
// POST: /UserActivity/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
/*
if (ModelState.IsValid)
{
userActivity.Add(useractivity);
*/
UserActivityModels useractivity = new UserActivityModels();
int Id = Convert.ToInt32(collection["Id"]);
int CreatedBy = Convert.ToInt32(collection["Created By"]);
DateTime CreatedOn = Convert.ToDateTime(collection["Created On"]);
int ModifiedBy = Convert.ToInt32(collection["Modified By"]);
DateTime ModifiedOn = Convert.ToDateTime(collection["Modified On"]);
string ContactId = collection["Contact Id"];
int StatusCode = Convert.ToInt32(collection["Status Code"]);
int StateCode = Convert.ToInt32(collection["State Code"]);
string EntityName = collection["Entity Name"];
int EntityId = Convert.ToInt32(collection["Entity Id"]);
DateTime DueDate = Convert.ToDateTime(collection["Due Date"]);
DateTime ActualEndDate = Convert.ToDateTime(collection["Actual EndDate"]);
DateTime MasqueradeOn = Convert.ToDateTime(collection["Masquerade On"]);
DateTime MasqueradeBy = Convert.ToDateTime(collection["Masquerade By"]);
string ContactName = collection["Contact Name"];
userActivity.Add(useractivity);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//---------------------------------------------------------------------//
// GET: /UserActivity/Edit/5
public ActionResult Edit(int id)
{
//ConfigurationManager.ConnectionStrings["CRMPORTALSQLCONN"].ConnectionString);
string query = @"
SELECT ua.Id
,ua.CreatedBy
,ua.CreatedOn
,ua.ModifiedBy
,ua.ModifiedOn
,ua.ContactId
,ua.EntityName
,ua.EntityId
,ua.StatusCode
,ua.StateCode
,ua.ActivityType
,ua.ActivityStatus
,ua.DueDate
,ua.ActualEndDate
,ua.MasqueradeOn
,ua.MasqueradeBy
,concat(c.FirstName, ' ', c.LastName) AS ContactName
FROM UserActivity ua
INNER JOIN Contact c ON c.Id = ua.ContactId
WHERE ua.id = @userActivityId
";
UserActivityModels userActivity = null;
using (IDbConnection db = new MySqlConnection(ConfigurationManager.
ConnectionStrings["CRMPORTALSQLCONN"].ConnectionString))
{
// userActivity = (UserActivityModels)db.Query<UserActivityModels>(query);
userActivity = db.Query<UserActivityModels>(query
, new
{
@UserActivityId = id
}).FirstOrDefault();
}
return View(userActivity);
}
// POST: /UserActivity/Edit/
[HttpPost]
public ActionResult Edit()
{
if (ModelState.IsValid)
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
return View(userActivity);
}
//------------------------------------------------------------------------//
// GET: /UserActivity/Delete/
public ActionResult Delete(int? id)
{
return View();
}
// POST: /SavedCar/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
userActivity Model:
public class UserActivityModels
{
[Display(Name = "Id")]
public int Id { get; set; }
[Display(Name = "Created By")]
public int CreatedBy { get; set; }
[Display(Name = "Created On")]
[DataType(DataType.DateTime)]
public DateTime CreatedOn { get; set; }
[Display(Name = "Modified By")]
public int ModifiedBy { get; set; }
[Display(Name = "Modified On")]
[DataType(DataType.DateTime)]
public DateTime ModifiedOn { get; set; }
[Display(Name = "Contact Id")]
public string ContactId { get; set; }
[Display(Name = "Entity Name")]
public string EntityName { get; set; }
[Display(Name = "Entity Id")]
public int EntityId { get; set; }
[Display(Name = "Status Code")]
public int StatusCode { get; set; }
[Display(Name = "State Code")]
public int StateCode { get; set; }
[Display(Name = "Activity Type")]
public int ActivityType { get; set; }
[Display(Name = "Activity Status")]
public string ActivityStatus { get; set; }
[Display(Name = "DueDate")]
[DataType(DataType.DateTime)]
public DateTime DueDate { get; set; }
[Display(Name = "Actual EndDate")]
[DataType(DataType.DateTime)]
public DateTime ActualEndDate { get; set; }
[Display(Name = "Masquerade On")]
[DataType(DataType.DateTime)]
public DateTime MasqueradeOn { get; set; }
[Display(Name = "Masquerade By")]
public int MasqueradeBy { get; set; }
[Display(Name = "Contact Name")]
public string ContactName { get; set; }
答案 0 :(得分:1)
在您的更新方法中,您应该抓住模型然后将其提供给数据库。
以下是几个简单的例子:
[Post]
public ActionResult Add(User u) {
// Need to define 'db' as a Data connection
db.Users.Add(u);
db.SubmitChanges();
return View(u);
}
[Post]
public ActionResult Update(User u) {
// Need to define 'db' as a Data connection
var updatedUser = db.Users.Where(k=>k.Id == u.Id).FirstOrDefault();
updatedUser = u;
db.SubmitChanges();
return View(updatedUser);
}
答案 1 :(得分:0)
仅向userActivity
添加元素不会更新数据库。每次尝试显示页面时,它都会重新创建新控制器,还有一个新的userActivity 。要解决此问题,必须在Create Action
中将新的使用率添加到数据库中,而不是添加到userActivity
。
答案 2 :(得分:0)
在创建帖子控制器中,您有
int Id = Convert.ToInt32(collection["Id"]);
如果Id是自动递增,则只需输入int Id = 0或者您不需要提供它。如果您自己提供Id,则可能会重复并且无法执行插入操作。 关于编辑视图,隐藏Id非常重要,这样您才能真正更新正确的ID。请检查以下内容: - 你的mysql表结构 - 在创建视图中删除Id字段 - 在编辑视图中添加隐藏的Id字段 - 检查你的控制器。