简体   繁体   English

提交后将输入的数据存储在SqlServer中

[英]Storing entered data in SqlServer after submit

Here I have am creating a small application. 在这里,我正在创建一个小应用程序。 Where I have this HTML page where I enter the data. 我在此HTML页面输入数据的位置。 On clicking Submit button the data should get stored in the database through a stored procedure. 单击提交按钮后,数据应通过存储过程存储在数据库中。 I have written insert statement in stored procedure. 我在存储过程中写了插入语句。

html code: html代码:

@model  USTGlobal.WorkBench.UI.Models.FloorPlanViewModel 
@{
    ViewBag.Title = "RequestForm";

}
<div>
    <div class="row">
        <div class="col-lg-12">
            <h2 class="page-header">Request Form</h2>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <div class="form-horizontal">
                <div class="form-group">
                    <label class="control-label col-lg-4">Period:</label>
                    <div class="col-lg-8">
                        @Html.DropDownList("Quarter", new SelectListItem[] { (new SelectListItem() { Text = "Q1", Value = "1"}), (new SelectListItem() { Text = "Q2", Value = "2" }), (new SelectListItem() { Text = "Q3", Value = "3" }), (new SelectListItem() { Text = "Q4", Value = "4"}) }, "-- Select Quarter --", new { @class = "form-control" })
                       <br />
                         @Html.DropDownList("Year", new SelectListItem[] { (new SelectListItem() { Text = "2016", Value = "2016" }), (new SelectListItem() { Text = "2017", Value = "2017" }) }, "-- Select Year --", new { @class = "form-control" })
                     </div>
                </div>
            </div>           
            <div class="form-horizontal">
                    <div class="form-group">
                        <label class="control-label col-lg-4">Line ID:</label>
                        <div class="col-lg-8">
                           @Html.TextBoxFor(model => model.floorConfig.LineID, new { onkeypress = "return isNumberKey(event)", @class = "form-control" })
                        </div>
                    </div>
            </div>  

            <div class="form-horizontal">
                <div class="form-group">
                    <label class="control-label col-lg-4">Project:</label>
                    <div class="col-lg-8">
                       @Html.TextBoxFor(model => model.floorConfig.Project, new { onkeypress = "return isNumberKey(event)", @class = "form-control" })
                    </div>
                </div>
            </div> 


            <div class="form-horizontal">
                <div class="form-group">
                    <label class="control-label col-lg-4">Budget:</label>
                    <div class="col-lg-8">
                      @Html.TextBoxFor(model => model.floorConfig.Budget, new { onkeypress = "return isNumberKey(event)", @class = "form-control" })
                    </div>
                </div>
            </div>        


        </div>
    </div>

    <div class="row">
        <div class="col-lg-offset-4" style="padding: 10px 0px 0px 0px;">
            <input type="submit" id="btnSubmit" value="Submit" class="btn btn-lg btn-success" />
            <input type="button" id="btnCancel" value="Clear" class="btn btn-lg btn-success" />
        </div>
    </div>

</div>

FloorPlanviewmodel: public FloorConfirguration floorConfig { get; set; } FloorPlanview模型: public FloorConfirguration floorConfig { get; set; } public FloorConfirguration floorConfig { get; set; }

Repository code: 仓库代码:

 public class FloorConfirguration
    {
        public string Quarter { get; set; }
        public int Year { get; set; }
        public string LineID { get; set; }
        public string Project { get; set; }
        public decimal Budget { get; set; }
    }

How to move forward from here? 如何从这里前进? Should I make the call to the stored procedure in controller ? 我应该在controller调用存储过程吗?

You can tell Entity Framework to execute a particular SQL Command using DbContext.Database.ExecuteSqlCommand 您可以使用DbContext.Database.ExecuteSqlCommand告诉Entity Framework执行特定的SQL命令

This method will execute any command you specify, whether its a string containing SQL code or the name of a Stored Procedure. 此方法将执行您指定的任何命令,无论它是包含SQL代码的字符串还是存储过程的名称。

ExecuteSqlCommand accepts an array for parameters as well so to execute Stored Procedure that takes parameter values (as yours should) for each new bit of data you want to insert, you could try this: ExecuteSqlCommand接受参数数组,因此要执行存储过程(对于您要插入的每个新数据位,都需要参数值)(您应该这样做),您可以尝试以下操作:

List<SqlParameter> args = new List<SqlParameter>();
args.Add(new SqlParameter("@parameterName", parameterValue));
// Keep adding parameters like this

using (MyDatabaseContext db = new MyDatabaseContext())
{
    db.Database.ExecuteSqlCommand("StoredProcedureName", args.ToArray());
}

This will be execute in the ActionResult function that your submit button posts to in your Controller. 这将在您的提交按钮发布到Controller中的ActionResult函数中执行。

You can use 您可以使用

View; 视图;

@using (Html.BeginForm("ActionName", "Controller", FormMethod.Post))
{
   //Add your desing
}

Controller; 控制器;

[HttpPost]
public ActionResult ActionName(Model model)
{
    if (ModelState.IsValid)
    {
        try
        {
            //Create SQL class and write your method (insert, update or delete etc.).
            bool check = SQLManager.Check(model);

            if (check)
            {
                //Make something
            }
            else
            {
                //Return error
                return View("Index");
            }
        }
        catch (Exception ex)
        {
            //Return error
            return View("Index");
        }
    }
    else
    {   
        //Return error
        return View("Index");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM