MVC5 Mqsql Issue

2019-06-09 08:12发布

After Update of Model from database in Entity Framework. Json Data not populate into textbox. when i use DeveloperTool i found a error "There is already an open DataReader associated with this Connection which must be closed first."[Error 505] Help me for resolve this problem.am using MySql in my project. When i use only one table in Model then i didn't get any error but when i update model then my project not working. If i add all the tables in Model then I face same problem.

Here is my code

Controller:-

// GET: Chains
public ActionResult Index()
{
    ViewData["chain_name"] = new SelectList(db.chains, "code", "name");
    return View(db.chains.ToList());
}

//Action Function callby javascript
[HttpPost]
public ActionResult Action(string code)
{
    var query = from c in db.chains
                where c.code == code
                select c;

    return Json(query);//Return Json Result
}

View:-

@using (@Html.BeginForm())
 {
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <hr />
        <div class="form-group">
            <label class="col-sm-2 control-label">
                Select Chain
            </label>
            <div class="col-md-3">
                @Html.DropDownList("ddlchainname", (SelectList)ViewData["chain_name"], new {         onchange = "Action(this.value);", @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
    <label class="col-sm-2 control-label">
        Chain Name
    </label>
    <div class="col-md-3">
        @Html.TextBox("ChainName", null, new { @class = "form-control" })
    </div>
    <label class="col-sm-2 control-label">
        Username
    </label>
    <div class="col-md-3">
        @Html.TextBox("username", null, new { @class = "form-control" })
            </div>
</div>
<div class="form-group">
    <label class="col-sm-2 control-label">
        Chain Code
   </label>
    <div class="col-md-3">
        @Html.TextBox("ChainCode", null, new { @class = "form-control" })
    </div>
</div>
    </div>
 }

<script type="text/javascript">
function Action(code) {
$.ajax({
    url: '@Url.Action("Action", "Chains")',
    type: "POST",
    data: { "code": code },
    "success": function (data) {
        if (data != null) {
            var vdata = data;
            $("#ChainName").val(vdata[0].name);
            $("#ChainCode").val(vdata[0].code);
            $("#username").val(vdata[0].username);
        }
    }
});

}

1条回答
小情绪 Triste *
2楼-- · 2019-06-09 08:55

Try this approach:

using (var db = new ChainEntities())
{
    ViewData["chain_name"] = new SelectList(db.chains, "code", "name");

    return View(db.chains.ToList());
}

This way you open the connection only once then dispose when done.

Sane for action:

[HttpPost]
public ActionResult Action(string code)
{

    using (var db = new ChainEntities())
    {
      var query = from c in db.chains
                  where c.code == code
                  select c;

      return Json(query);//Return Json Result
    }

}
查看更多
登录 后发表回答