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

时间:2014-11-01 14:37:48

标签: c# asp.net-mvc database button model-view-controller

我有一个数据库,我希望fill使用填充程序 - method。我希望在我method项目的Index - 视图中调用此MVC。我已经测试了method每次运行index.cshtml时都运行它,并填充table。唯一的问题是,如果我refreshindextable再次填充完全相同的data。这导致我获得了每个项目中的两个。

所以我得出结论,我想将filler()-method绑定到索引页面上的按钮以填充table。我的问题是:

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

额外信息: fill-method在其自己的类中称为DBFiller.cs,当我将其设置为在索引启动时运行时,我在actionresult的{​​{1}}中添加了DBfiller.filler()。

代码:
DBFiller.cs

controller

我尝试在用户控制器的索引启动器中添加它:

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);

    }

}
}

Index.cshtml如果有帮助:

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);
    }
}
}

尝试JS的填充调用:

@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>


}

控制器:

@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>

1 个答案:

答案 0 :(得分:0)

您可以将填充逻辑移动到另一个操作方法,并在单击按钮时使用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);

    });
});

使用相应的网址和按钮ID

在页面中添加上述代码