System.InvalidOperationException:'由于已经处理了DbContext,因此无法完成操作。

时间:2017-07-17 10:36:37

标签: asp.net-mvc

在视图中我遇到错误

这是我的控制器

using (var db=new wipoeeshetronEntities())
        {
            ViewBag.Role = new SelectList(db.masterroledet_tbl.Where(m => m.IsDeleted == 0), "RoleID", "RoleName");
            var RoleplayModule = db.masterrolemodulelink_tbl.Where(e => e.IsDeleted == 0);
            return View(RoleplayModule.ToList());  
        }

这是我的观点

@Html.DropDownList("RoleId1", (SelectList)ViewData["Role"], "--Select Role for Update--", new { @id = "RoleID1", @class = "btn btn-block orange dropdown-toggle ", @name = "RoleID1", @style = "width:20%;display:inline;float:right", @required = "required" })

1 个答案:

答案 0 :(得分:2)

在SelectList中添加ToList

package ie.example.artur.adminapp;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class MainActivity extends AppCompatActivity {


    EditText editTextName,editTextEmail,editTextPassword;
    TextView textView;
    private static final String DB_URL = "jdbc:mysql://192.168.1.6/socialmedia_website";
    private static final String USER = "zzz";
    private static final String PASS = "zzz";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        textView = (TextView) findViewById(R.id.textView);
        editTextName = (EditText) findViewById(R.id.editTextName);
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    public void btnConn(View view) {
        Send objSend = new Send();
        objSend.execute("");


    }

    private class Send extends AsyncTask<String, String, String>

    {
        String msg = "";
        String name = editTextName.getText().toString();
        String email = editTextEmail.getText().toString();
        String password = editTextPassword.getText().toString();

        @Override
        protected void onPreExecute() {
            textView.setText("Please Wait Inserting Data");
        }

        @Override
        protected String doInBackground(String... strings) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
                if (conn == null) {
                    msg = "Connection goes wrong";
                } else {
                String query = "Insert INTO test1 (name,email,password) VALUES('"+name+","+email+","+password+"')";
                Statement stmt = conn.createStatement();
                stmt.executeUpdate(query);
                msg = "Inserting Successful!!";

            }

                conn.close();

        }

        catch(
        Exception e
        )

        {
            msg = "Connection goes Wrong";
            e.printStackTrace();

        }

        return msg;


    }



@Override
    protected void onPostExecute(String msg) {textView.setText(msg);}



    }




}

问题是您传递视图未执行查询,当视图尝试执行上下文时已经使用