ASP EF Increment / Decrement Value with + / - buttons

时间:2016-04-25 09:10:13

标签: c# asp.net asp.net-mvc entity-framework

So I'm new to ASP and EF and I am wondering how to do this incredibly basic operation, as well as a few questions to go along with doing it.

Currently I have a table we will call Resource;

class Resource
{
    int current;
    int min;
    int max;
};

Right now I have the default CRUD options for this. What I would like is a + / - button on the main list that will manipulate the current value of each resource and update the value in the DB and on screen.

There are also certain operations I'd like to run such as "AddFive" to a selected group of resources.

So my questions;

  1. How do I do this?
  2. Is this scalable? If someone is constantly hitting the buttons this is obviously going to send a lot of requests to my DB. Is there any way to limit the impact of this?
  3. What are my alternatives?

Thanks.

Edit: To clarify the question; here are my post functions. How / where do I add these in my view to get a button showing for each resource. I just want the action to fire and refresh the value rather than redirect to a new page.

@Html.ActionLink("+", "Increment", new { id = item.ID })

public void Increment(int? id)
{
    if (id != null)
    {
        Movie movie = db.Movies.Find(id);
        if (movie != null)
        {
            Increment(movie);
        }
    }
}

[HttpPost, ActionName("Increment")]
[ValidateAntiForgeryToken]
public ActionResult Increment([Bind(Include = "ID,Title,ReleaseDate,Genre,Price")] Resource resource)
{
    if ((resource.Current + 1) < (resource.Max))
        resource.Current++;
    return View(resource);
}

1 个答案:

答案 0 :(得分:1)

这听起来像是你在前端创建电影列表并更新特定电影的详细信息。

这里的关键是你需要在每个项目周围包装一个表单并将该帖子发布到更新控制器或使用ajax / jquery来调用控制器。

我给你举了第一个例子。一旦更新控制器被点击,它将重定向到列表页面,然后将显示更新的电影列表。

以下是如何连线的最小工作示例。为简洁起见,我没有包含任何数据访问代码,但在评论中包含了伪代码,以向您显示放置它的位置。

如果您有任何进一步的问题,请告诉我。

<强>控制器

public class MoviesController : Controller
{
    public ViewResult Index()
    {
        // Data access and mapping of domain to vm entities here. 
        var movieListModel = new MovieListModel();
        return View(movieListModel);
    }

    public ActionResult Increment(IncrementMovieCountModel model)
    {
        // Put breakpoint here and you can check the value are correct
        var incrementValue = model.IncrementValue;
        var movieId = model.MovieId;

        // Update movie using entity framework here
        // var movie = db.Movies.Find(id);
        // movie.Number = movie.Number + model.IncrementValue;
        // db.Movies.Save(movie);

        // Now we have updated the movie we can go back to the index to list them out with incremented number
        return RedirectToAction("Index");
    }

}

查看

@model WebApplication1.Models.MovieListModel
@{
    ViewBag.Title = "Index";
}

<h2>Some Movies</h2>


    <table>
        <tr>
            <td>Id</td>
            <td>Name</td>
            <td>Increment Value</td>
            <td></td>
        </tr>

        @foreach (var movie in Model.MovieList)
        {
            using (Html.BeginForm("Increment", "Movies", FormMethod.Post))
            {
                 <tr>
                     <td>@movie.Id @Html.Hidden("MovieId", movie.Id)</td>
                     <td>@movie.Name</td>
                     <td>@Html.TextBox("IncrementValue", movie.IncrementValue)</td>
                     <td><input type="submit" name="Update Movie"/></td>
                 </tr>
             }
        }
    </table>

<强>模型

  public class MovieListModel
    {
        public MovieListModel()
        {
            MovieList = new List<MovieModel> {new MovieModel{Id=1,Name = "Apocalypse Now",IncrementValue = 3}, new MovieModel {Id = 2,Name = "Three Lions", IncrementValue = 7} };
        }

        public List<MovieModel> MovieList { get; set; }
    }

    public class MovieModel
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public int IncrementValue { get; set; }
    }

    public class IncrementMovieCountModel
    {
        public int IncrementValue { get; set; }
        public int MovieId { get; set; }
    }