-1

您好我正在尝试将数据插入到由用户填写的数据库中。我正在使用 Visual Studio 2010 Asp.net mvc 2。但我收到错误

找不到资源。/Fillcustomer/customerdetail
.但是服务器名称、用户名和密码是正确的。请更正我的代码。

客户.cs


 namespace displaycustomer.Models
{
    public class customer
    {
        public int id { set; get; }
        public string customercode { set; get; }
        public int amount { set; get; }   

    public int Insert(int cid,string ccode, int amount)
    {

        string sqlconn = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
        SqlConnection cn = new SqlConnection(sqlconn);
        cn.Open();           
        SqlCommand cmd = new SqlCommand("INSERT INTO tblcus(C_Id, C_Code, Amount) " +
            " VALUES('" + cid + "','" + ccode + "', '" + amount + "')", cn);
            return cmd.ExecuteNonQuery();           

    }
}

}

customerdetailcontroller.cs

    namespace displaycustomer.Controllers
{
    public class customerdetailController : Controller
    {
        //
        // GET: /customerdetail/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Fillcustomer()
        {
            return View();
        }

        [HttpPost]
        public ActionResult insert()
        {
            customer cus = new customer();
            int id = Convert.ToInt16(Request.Form["customerid"]);
            string code = Request.Form["customercode"].ToString();
            int amt = Convert.ToInt16(Request.Form["amount"]);
            int _records = cus.Insert(id, code, amt);
            if (_records > 0)
            {
                return RedirectToAction("customerdetail", "Fillcustomer");
            }
            else
            {
                ModelState.AddModelError("", "Can Not Insert");
            }
            return View(cus);
        }

    }
}

填写客户.aspx

<form method="post" action="insert">
        <table>
            <tr>
                <td>Customer Id</td>
                <td><input type="text" name="customerid" /></td>
             </tr>
             <tr>
                <td>Customer Code</td>
                <td><input type="text" name="customercode" /></td>
             </tr>
             <tr>
                <td>Amount</td>
                <td><input type="text" name="amount" /></td>
             </tr>
             <tr>
                <td colspan="2"><input type="submit" value="Submit"/></td>
             </tr>
        </table>
    </form>

网络配置

<connectionStrings>
<add name="ConnString" connectionString="server=servername; Initial Catalog=sample; UID=user; pwd=123; Integrated Security=True;" providerName="System.Data.SqlClient" />

4

1 回答 1

0

我想问题出在连接字符串上。我曾经使用过这种格式。也许这可以帮助你

<add name="ProductEntities" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFileName=|DataDirectory|\Sam.mdf;Password=12345;User Id=ABC;Integrated Security=SSPI" />

文件 Sam.mdf 在 App_Data 文件夹中的位置。

好的,现在您的网址不正确。

它的格式应为 /Controller Name/Method Name

它应该是

 /customerdetail/FillCustomer

不是

/Fillcustomer/customerdetail

改变这个

 public ActionResult insert()
    {
        customer cus = new customer();
        int id = Convert.ToInt16(Request.Form["customerid"]);
        string code = Request.Form["customercode"].ToString();
        int amt = Convert.ToInt16(Request.Form["amount"]);
        int _records = cus.Insert(id, code, amt);
        if (_records > 0)
        {
            return RedirectToAction("Fillcustomer", "customerdetail");//change this
        }
        else
        {
            ModelState.AddModelError("", "Can Not Insert");
        }
        return View(cus);
    }

RedirectToAction 方法将第一个参数作为动作名称,将第二个参数作为控制器名称

于 2013-11-09T06:03:59.887 回答