MVC5 Mqsql问题 [英] MVC5 Mqsql Issue

查看:164
本文介绍了MVC5 Mqsql问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实体框架从数据库模型的更新后。 JSON数据不填充到文本框。当我使用DeveloperTool我发现了一个错误的已经有与此连接,必须先关闭相关联的打开DataReader的。[错误505] 帮助我解决使用MySQL这个problem.am在我的项目。当我在模型中使用只有一张桌子,然后我没有得到任何错误,但是当我更新模型,然后我的项目不能正常工作。如果我加入模型中的所有表,然后我面临同样的问题。

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.

下面是我的code

控制器: -

// 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
}

查看 -

@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);
        }
    }
});

}

推荐答案

试试这个办法:

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.

理智的行动:

[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
    }

}

这篇关于MVC5 Mqsql问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆