gpt4 book ai didi

c# - ObjectContext 实例已被释放,不能再用于需要连接的操作

转载 作者:IT王子 更新时间:2023-10-29 03:57:20 27 4
gpt4 key购买 nike

我的看法是:

@model MatchGaming.Models.ProfileQuery
@{
ViewBag.Title = "Index";
}

<h2>Index</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm("Results", "Profiles")) {
@Html.ValidationSummary(true)
<fieldset>
<legend>ProfileQuery</legend>
@Html.EditorFor(model=>model.SearchString)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

我有这个用于 HttpPost 的 Controller :

[HttpPost]
public ActionResult Results(ProfileQuery profileQuery)
{
Debug.Write(profileQuery.SearchString);
using(var db = new MatchGamingEntities())
{
var SearchUserName = db.Users.SingleOrDefault(a=> a.UserName.Contains(profileQuery.SearchString));
var Users = from m in db.Users
join m2 in db.MyProfiles on m.UserId equals m2.UserId
where m.UserName == SearchUserName.UserName
select new UserViewModel
{
UserName = m.UserName,
LastActivityDate = m.LastActivityDate,
Address = m2.Address,
City = m2.City,
State = m2.State,
Zip = m2.Zip
};

return View(Users.AsEnumerable());
}
}

这是结果 View :

@model IEnumerable<MatchGaming.Models.UserViewModel>    
@{
ViewBag.Title = "Results";
}

<h2>Results</h2>

<fieldset>
<legend>UserViewModel</legend>
@foreach (var item in Model){
<div class="display-label">UserName</div>
<div class="display-field">@item.UserName</div>

<div class="display-label">LastActivityDate</div>
<div class="display-field">@String.Format("{0:g}", item.LastActivityDate)</div>

<div class="display-label">Address</div>
<div class="display-field">@item.Address</div>

<div class="display-label">City</div>
<div class="display-field">@item.City</div>

<div class="display-label">State</div>
<div class="display-field">@item.State</div>

<div class="display-label">Zip</div>
<div class="display-field">@item.Zip</div>
}
</fieldset>

我一直收到这个错误:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

我不明白为什么。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com