如何在asp.net mvc2中的SQL数据库中插入文本框值 [英] How to textbox value Insert in SQL database in asp.net mvc2

查看:51
本文介绍了如何在asp.net mvc2中的SQL数据库中插入文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



如何解决我的问题。我想使用asp.net mvc2将数据插入到sql数据库中。但是我遇到了一些问题...



我的asp.net mvc代码....



查看表单名称:FrmCompany.aspx

代码:



Hi every one,

How to solve my problem. I want Insert data into sql databse using asp.net mvc2. but i get some problem...

My asp.net mvc code....

View Form Name : FrmCompany.aspx
Code:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Top.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    FrmCompany
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <link href="../../Content/css/FormStyle.css" rel="stylesheet" type="text/css" />


<form id="Form1" method="post" action="CompanyController/Insert" runat="server">

<% using (Html.BeginForm(null, null, FormMethod.Get, new { name = "FrmCompany", id = "Form1" }))    //"FrmCompany", "CompanyController"
   { %>
    <div class="form-body">
        <div class="form-body-title">
            <h2>Company Information</h2>
        </div>

        <div class="form-body-inner">
            <div class="form-body-label"> Company Name </div> <input type="text" name="txtCustomerName" style="width:358px" />  <div class="br-clear"></div>
            <div class="form-body-label"> Address </div> <input type="text" name="txtAddress" style="Width:358px; Height:94px;" />  <div class="br-clear"></div>
            <div class="form-body-label"> Post Code </div> <input type="text" name="txtPostCode" style="width:100px" />  <div class="br-clear"></div>
            <div class="form-body-label"> City </div> <input type="text" name="txtCity" style="width:132px" />  <div class="br-clear"></div>
            <div class="form-body-label"> Phone </div> <input type="text" name="txtPhone" style="width:358px" />  <div class="br-clear"></div>
            <div class="form-body-label"> Fax </div> <input type="text" name="txtFax" style="width:229px" />  <div class="br-clear"></div>
            <div class="form-body-label"> Email </div> <input type="text" name="txtEmail" style="width:358px" />  <div class="br-clear"></div>
            <div class="form-body-label"> Web </div> <input type="text" name="txtWeb" style="width:150px" />

            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            <asp:DropDownList ID="cboStatus" runat="server">
                <asp:ListItem>Head Office</asp:ListItem>
                <asp:ListItem>Branch Office</asp:ListItem>
            </asp:DropDownList>

            <br /> <br /> <br />

            <div class="form-body-label"> Look In </div>
                <asp:DropDownList ID="DropDownList1" runat="server" Width="358">
                    <asp:ListItem>Head Office</asp:ListItem>
                    <asp:ListItem>Branch Office</asp:ListItem>
                </asp:DropDownList>

            <br /> <br />

            <div class="form-button">
                <div class="form-button-text">
                <input type="submit" value="Insert" id="Insert" style="width:80px;"/>
                    <% } %>
                    <input type="submit" value="Reset" style="width:80px;"/>
                </div>
            </div>





        </div>
    </div>
    </form>
</asp:Content>





并使用控制器名称:



And Using Controller Name:

CompanyController.cs







using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC_Bridal.Models;

namespace MVC_Bridal.Controllers
{
    public class CompanyController : Controller
    {
        //
        // GET: /Company/

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

        [HttpPost]
        public ActionResult Insert()
        {
            MVC_Bridal.Models.InsertModel insertmodel = new InsertModel();

            string txtCustomerName = Request.Form["txtCustomerName"];
            string txtAddress = Request.Form["txtAddress"];
            string txtPostCode = Request.Form["txtPostCode"];
            string txtCity = Request.Form["txtCity"];
            string txtPhone = Request.Form["txtPhone"];
            string txtFax = Request.Form["txtFax"];
            string txtEmail = Request.Form["txtEmail"];
            string txtWeb = Request.Form["txtWeb"];
            //string cboStatus = Request.Form["cboStatus"];

            int _records = insertmodel.Insert(txtCustomerName, txtAddress, txtPostCode, txtCity, txtPhone, txtFax, txtEmail, txtWeb);
            if (_records > 0)
            {
                return RedirectToAction("FrmCompany", "Home");
            }
            else
            {
                ModelState.AddModelError("", "Can Not Insert");
            }


            return View(insertmodel);
        }

    }
}





还使用型号名称:



And also using Models name:

InsertModel .cs







public int Insert(string CustomerName, string Address, string PostCode, string City, string Phone, string Fax, string Email, string Web)
       {
           SqlConnection cn = new SqlConnection("Data Source= SERVER;Initial Catalog= Cosmic_GulshanBranch;User ID = sa; Password = 123;");
           SqlCommand cmd = new SqlCommand("INSERT INTO tbl_Company(CompanyName, CompanyAddress, PostCode, City, CompanyPhone, CompanyFax, CompanyEmail, CompanyWeb) " +
               " VALUES('" + CustomerName + "','" + Address + "', '" + PostCode + "','" + City + "', '" + Phone + "','" + Fax + "', '" + Email + "','" + Web + "')", cn);
           cn.Open();
           return cmd.ExecuteNonQuery();

       }







但不要插入数据sql数据库。



问题:




But do not insert data into sql database.

Problem :

The HTTP verb POST used to access path '/Home/FrmCompany/CompanyController/Insert' is not allowed.

推荐答案

创建一个参数作为FormCollection。

然后您可以在FormCollection中通过id找出输入控件的值。

尝试一次FormCollection然后你可以进入你的函数。
Create a parameter as FormCollection.
then You can find out the values of your input control by id in FormCollection.
Try FormCollection once and then you can pass into your function.


这篇关于如何在asp.net mvc2中的SQL数据库中插入文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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