2014-11-01 102 views
1

我有一个数据库,我想fill与填料-method。我希望从Index - 我的MVC-项目中查看method。我已经测试method每次运行index.cshtml时都会运行它,并且它会填充table。唯一的问题是,如果我refreshindex页,table得到完全相同的data再次填充。这导致我得到每个项目的两个。如何填充数据库buttonclick

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

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

额外的信息: 填充法是在其名为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 = "[email protected]", 
      Password = "mhmhmh", 
      Phonenr = "93929392", 
      Postcode = "9382", 
      cities = city1 
     }; 

     var user2 = new Users() 
     { 
      Address = "Bakvendtgata 8", 
      Firstname = "Hanne", 
      Surname = "Lande", 
      Email = "[email protected]", 
      Password = "hlhlhl", 
      Phonenr = "82711212", 
      Postcode = "2301", 
      cities = city2 
     }; 

     var user3 = new Users() 
     { 
      Address = "Fisefin Aveny 14", 
      Firstname = "Voldemort", 
      Surname = "Olsen", 
      Email = "[email protected]", 
      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"); 
    } 

回答

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

    }); 
}); 

在你的页面添加上面的代码与相应的URL和按钮的ID

+0

其实从来没有使用过阿贾克斯之前的代码,你能否提供一些源代码或相关链接?谢谢 – mathletics 2014-11-01 14:56:25

+0

我已经添加了一个链接和一些示例代码。看看是否有帮助。 – Jags 2014-11-01 15:20:45

+0

它似乎没有工作,我可能做错了东西。 – mathletics 2014-11-01 15:57:21