我正在尝试在“UserController”中保存一个值,但我似乎无法保存到数据库中。
我可以获取存储在UserController中的数据库值以进行更改,以便在提交值并刷新视图时,它看起来好像已存储。 (即,从测试更改为测试)但是,只要我再次刷新页面,它就会返回到先前的值,并且数据库在整个过程中都没有显示任何变化。
UserController中:
var g = httpPost.VanityUrl;
DB.ppUser_editUser(g);
ppUser中的ppUser_editUser:
public ppUser ppUser_editUser(string personVanity)
{
ppUser user = SessionUser.LoggedInUser.Person;
user.VanityUrl = personVanity;
BaseDB.SubmitChanges();
return user;
}
EditProfile查看:
<div>
@Html.LabelFor(m => m.VanityUrl, "Edit your Custom Url:")
@Html.TextBoxFor(m => m.VanityUrl, new { maxlength = 15 })
</div>
BaseDB:
public partial class StudioKit_MVC3Repository : IDisposable
{
private StudioKit_MVC3ModelsDataContext _BaseDB;
public StudioKit_MVC3ModelsDataContext BaseDB
{
get
{
if (_BaseDB == null)
{
if (StudioKit_MVC3Config.EnvironmentDetail.Name == "Production")
{
_BaseDB = new StudioKit_MVC3ModelsDataContext(StudioKit_MVC3Config.EnvironmentDetail.ConnectionString);
}
else
{
var sqlConnection = new SqlConnection(StudioKit_MVC3Config.EnvironmentDetail.ConnectionString);
var conn = new ProfiledDbConnection(sqlConnection, MiniProfiler.Current);
_BaseDB = new StudioKit_MVC3ModelsDataContext(conn);
}
}
return _BaseDB;
}
}
}
编辑: 我解决了这个问题。我将值保存到会话用户实例而不是实际数据库。我摆脱了方法,在控制器中我简单地说:
ppUser check = DB.ppUser_GetUserByPersonID(model.cskPersonID);
check.VanityUrl = httpPost.VanityUrl;
DB.Save();