如何从按钮点击填充数据库

本文关键字:填充 数据库 按钮 | 更新日期: 2023-09-27 18:28:38

我有一个数据库,我想用填充符fillmethod。我希望在我的MVC-项目中从Index-视图调用这个method。我已经测试了method的工作原理,每次运行index.cshtml时都运行它,它会填充table。唯一的问题是,如果我refresh index-页面,那么table将再次填充完全相同的data。这让我得到了每件物品中的两件。

因此,我得出的结论是,我想将我的filler()-method绑定到索引页上的一个按钮来填充table。我的问题是:

如何将方法直接绑定到project中的按钮?

额外信息:filler方法在它自己的名为DBFiller.cs的类中,当我将它设置为在索引启动时运行时,我在controlleractionresult中添加了DBFiller.filler()。

代码:
DBFiller.cs

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Oblig1.BLL;
using Oblig1.Model;
using Oblig1.DAL;
namespace Oblig1
{
public class DBFiller
{
    public void Filler()
    {
        var userDb = new UserBLL();
        var itemDb = new ItemBLL();
        var city1 = new Cities();
        var city2 = new Cities();
        var city3 = new Cities();
        city1.Cityname = "Oslo";
        city1.Postcode = "9382";
        city2.Cityname = "Trondheim";
        city2.Postcode = "2301";
        city3.Cityname = "Bergen";
        city3.Postcode = "1723";
        var user1 = new Users()
        {
            Address = "Moroveien 7",
            Firstname = "Martin",
            Surname = "Hermansen",
            Email = "mh@mh.no",
            Password = "mhmhmh",
            Phonenr = "93929392",
            Postcode = "9382",
            cities = city1
        };
        var user2 = new Users()
        {
            Address = "Bakvendtgata 8",
            Firstname = "Hanne",
            Surname = "Lande",
            Email = "hl@hl.no",
            Password = "hlhlhl",
            Phonenr = "82711212",
            Postcode = "2301",
            cities = city2
        };
        var user3 = new Users()
        {
            Address = "Fisefin Aveny 14",
            Firstname = "Voldemort",
            Surname = "Olsen",
            Email = "vo@vo.no",
            Password = "vovovo",
            Phonenr = "12672891",
            Postcode = "1723",
            cities = city3
        };
        var item1 = new Items()
        {
            Currentstock = 40,
            Itemname = "Blanding",
            Itemprice = 99,
            Itemtype = "Pasta",
            PictureURL = "/img/pasta_blanding.png",
            Itemid = 78
        };
        var item2 = new Items()
        {
            Currentstock = 17,
            Itemname = "Fusilli",
            Itemprice = 119,
            Itemtype = "Pasta",
            PictureURL = "/img/pasta_fusilli.png",
            Itemid = 63
        };
        var item3 = new Items()
        {
            Currentstock = 80,
            Itemname = "Farfalle",
            Itemprice = 69,
            Itemtype = "Pasta",
            PictureURL = "/img/pasta_farfalle.png",
            Itemid = 30
        };
        var item4 = new Items()
        {
            Currentstock = 63,
            Itemname = "Colored Fusilli #1",
            Itemprice = 45,
            Itemtype = "Pasta",
            PictureURL = "/img/pasta_fusilli_farget.png",
            Itemid = 22
        };
        var item5 = new Items()
        {
            Currentstock = 3,
            Itemname = "Colored Fusilli #2",
            Itemprice = 33,
            Itemtype = "Pasta",
            PictureURL = "/img/pasta_fusilli_farget2.png",
            Itemid = 98
        };
        var item6 = new Items()
        {
            Currentstock = 77,
            Itemname = "Macaroni",
            Itemprice = 25,
            Itemtype = "Pasta",
            PictureURL = "/img/pasta_macaroni.png",
            Itemid = 76
        };
        var item7 = new Items()
        {
            Currentstock = 80,
            Itemname = "Measure",
            Itemprice = 299,
            Itemtype = "Tools",
            PictureURL = "/img/pasta_messure.png",
            Itemid = 59
        };
        var item8 = new Items()
        {
            Currentstock = 14,
            Itemname = "Multitool",
            Itemprice = 499,
            Itemtype = "Tool",
            PictureURL = "/img/pasta_multitool.png",
            Itemid = 69
        };
        var item9 = new Items()
        {
            Currentstock = 88,
            Itemname = "Penne",
            Itemprice = 19,
            Itemtype = "Pasta",
            PictureURL = "/img/pasta_penne.png",
            Itemid = 79
        };
        var item10 = new Items()
        {
            Currentstock = 45,
            Itemname = "Roller",
            Itemprice = 59,
            Itemtype = "Tool",
            PictureURL = "/img/pasta_ruller.png",
            Itemid = 79
        };
        var item11 = new Items()
        {
            Currentstock = 179,
            Itemname = "Spoon",
            Itemprice = 39,
            Itemtype = "Tool",
            PictureURL = "/img/pasta_sleiv.png",
            Itemid = 110
        };
        var item12 = new Items()
        {
            Currentstock = 17,
            Itemname = "Spagetti",
            Itemprice = 9,
            Itemtype = "Pasta",
            PictureURL = "/img/pasta_penne.png",
            Itemid = 79
        };

        itemDb.registerItem(item1);
        itemDb.registerItem(item2);
        itemDb.registerItem(item3);
        itemDb.registerItem(item4);
        itemDb.registerItem(item5);
        itemDb.registerItem(item6);
        itemDb.registerItem(item7);
        itemDb.registerItem(item8);
        itemDb.registerItem(item9);
        itemDb.registerItem(item10);
        itemDb.registerItem(item11);
        itemDb.registerItem(item12);
        userDb.setIn(user1);
        userDb.setIn(user2);
        userDb.setIn(user3);
    }
}
}

我尝试将其添加到我的用户控制器中的索引启动器中:

namespace Oblig1.Controllers
{
public class UserController : Controller
{
    public ActionResult Index()
    {
        var model = new UserBLL();
        var filler = new DBFiller();
        filler.Filler();
        List<Item> allItems = model.getItems();
        return PartialView(allItems);
    }
}
}

Index.chtml(如果有帮助的话):

@using Oblig1
@model IEnumerable<Oblig1.Model.Item>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<br />
<br />
<br />
<head>
<title>Index</title>
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
</head>
@if (Model != null)
{
<div class="row">
    @foreach(var item in Model)
    { 
    <div class="col-sm-6 col-md-3">
        <div class="thumbnail">
            <img src="@item.pictureURL" alt="...">
            <div class="caption">
                <h3>@item.itemname</h3>
                <p>Antall på lager: @item.currentstock<br />kr @item.itemprice,00</p>
                <button class="btn btn-success">@Html.ActionLink("Add to Cart", "AddToCart", new { id = item.itemid }, null)</button>


            </div>
        </div>
    </div>
    }
</div>

}

JS尝试填充调用:

@model IEnumerable<Oblig1.Model.Item>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<br />
<br />
<br />
<head>
<title>Index</title>
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script type="text/jscript">
$('#fill').click(function () {
    var url = "/UserController/Filler";
    $.post( url);
});
</script>
</head>
<button class="btn btn-success" id="fill" value="Fill Database"> </button>

控制器:

public ActionResult Filler()
    {
        var filler = new DBFiller();
        filler.Filler();
        return View("Index");
    }

如何从按钮点击填充数据库

您可以将填充的逻辑移动到另一个操作方法,并在单击按钮时使用jquery Ajax调用来调用它

编辑:

请参阅以下链接以获取样本

http://www.itorian.com/2013/02/jquery-ajax-get-and-post-calls-to.html

基本上下面是链接你的按钮点击与行动方法的代码

<script type="text/javascript">
 $(document).ready(function()
{
    $('#ButtonID').click(function () {
        var url = "/ControllerName/YourActionMethod";
        $.post( url);
    });
});

在您的页面中添加上述代码,并使用适当的url和按钮id