【问题标题】:How to fill database from buttonclick如何从 buttonclick 填充数据库
【发布时间】:2014-11-01 14:37:47
【问题描述】:

我有一个数据库,我想用填充符fill 填写-method。我希望从我的MVC-project 中的Index-view 调用这个method。我已经测试了method 每次运行index.cshtml 时都会运行它,它会填充table。唯一的问题是,如果我refreshindex-page,table 再次被完全相同的data 填充。这导致我得到了每件物品中的两个。

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

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

额外信息: 填充方法在其自己的名为 DBFiller.cs 的类中,当我将其设置为在索引启动时运行时,我在 actionresultcontroller 中添加了 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.cshtml 如果有帮助的话:

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

【问题讨论】:

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


    【解决方案1】:

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

    编辑:

    示例请参考以下链接

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

    基本上下面是将点击按钮与 Action 方法联系起来的代码

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

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

    【讨论】:

    • 实际上之前从未使用过 Ajax,您能否提供一些源代码或相关内容的链接?谢谢
    • 我添加了一个链接和一些示例代码。看看是否有帮助。
    • 它似乎不起作用,我可能做错了什么。
    • 您能否将您的 javascript / jquery 代码粘贴到您正在进行 jquery 调用的位置以及您的具有操作方法的控制器
    • - 那么按钮点击事件有效吗?我的意思是单击按钮时会调用 Filler 方法。您还可以删除 Filler 操作方法的返回类型,即您可以将其保留为 void Filler() 并删除返回语句
    猜你喜欢
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 2012-06-25
    • 2011-07-01
    • 2021-10-20
    • 1970-01-01
    相关资源
    最近更新 更多