像处理并与使用AJAX在ASP.NET MVC的实体 [英] Handling Like and Unlike of an entity in ASP.NET MVC using AJAX

查看:97
本文介绍了像处理并与使用AJAX在ASP.NET MVC的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个剃刀图,其中型号产品,所以我只是列出所有产品的使用模型上的foreach循环的观点的IEnumerable。随着每一个产品你可以看到下面,我展示的有多少ThumbsUps具有计数。我试图完成的最后一件事是,当他们点击链接给竖起大拇指:我想一个ProductThumbsUp记录添加到数据库登录的用户的电流。如果用户已经给予了赞许,并再次点击它,我想从产品删除其ProductThumbsUp。

我使用的EntityFramework 7和MVC6。我有两个表:产品和ProductThumbsUp

我想知道我应该怎么去增加对登录用户当他们点击使用MVC的链接电流的ProductThumbsUp记录。我假设这样的事情,这绝对是一个AJAX职位。不过,如果我只是只是想产品ID发送到AJAX职位?然后在我的控制器创建一个新的ProductThumbsUp并设置它的产品编号。然后同时发送新创建的ProductThumbsUp对象和当前登录的用户到我的仓库层保存。

如果有更好的方法来做到这一点,因为在我的模型每个岗位已经有了连接到它ThumbsUps的名单我想知道。有没有更好的办法,我可以这样做?

这里是我的Razor视图是如何设置:

  @model IEnumerable的<产品与GT;
@foreach(以型号VAR产品)
{
    < H2> @ product.Name< / H>
    < D​​IV> @ product.Description< / DIV>
    < A HREF =#类=lnkViewLikes>大拇指起坐#给出:;跨度类=徽章> @ product.ThumbsUps.Count()< / SPAN>< / A>    < A HREF =#类=lnkThumbsUp>给竖起大拇指< / A>
}

这里是我的产品型号如下:

 公众诠释标识{搞定;组; }
公共字符串名称{;组; }
公共字符串描述{搞定;组; }公众的ICollection< ProductThumbsUp> ThumbsUps {搞定;组; }


解决方案

要使其高效,

我会建议让所有的产品,而不是领thumbsUps的所有行从DB每一个产品,只是让每个产品等等计数。

当你不需要thumbsUp的每一行,但只是显示计数。想象一下,规模情况下,如果你有一个产品一百万喜欢,你突然从DB拉一百万行到你的服务器 - 只是为了显示计数

此外,我将会对数据库中的第二个电话来获得当前用户已经给所有thumbsUps(他喜欢的产品ID的列表)。这将允许您显示给用户,如果他们喜欢它,或者现在​​。

要加起来的acutal大拇指产品 - 你可以发送一个Ajax调用它告诉它的ProductID他喜欢和MVC的行动可以插入一个thumbsUp一行到数据库

I have a razor view where the model is an IEnumerable of Product so I am simply listing all of the products in the view using a foreach loop on the Model. Along with each product you can see below that I am showing the count of how many ThumbsUps it has. The final thing I am trying to accomplish is when they click on the link 'Give Thumbs Up' I would like to add a ProductThumbsUp record to the database for the current logged in user. If the user already has given a thumbs up and clicks it again I would like to remove their ProductThumbsUp from the product.

I am using EntityFramework 7 and MVC6. I have two tables: Product and ProductThumbsUp.

I would like to know how I should go about adding a ProductThumbsUp record for the current logged in user when they click the link using MVC. I am assuming something like this should definitely be an AJAX post. But would I simply just want to send the ProductId to the AJAX post? Then in my controller create a new ProductThumbsUp and set the ProductId of it. Then send both the newly created ProductThumbsUp object and the current logged in user down to my repository layer for saving.

I am wondering if there are better ways to do this since each post in my model already has a list of ThumbsUps attached to it. Is there a better way I can be doing this?

Here is how my razor view is setup:

@model IEnumerable<Product>
@foreach (var product in Model)
{
    <h2>@product.Name</h2>
    <div>@product.Description</div>
    <a href="#" class="lnkViewLikes"># of Thumbs Ups Given:<span class="badge">@product.ThumbsUps.Count()</span></a>

    <a href="#" class="lnkThumbsUp">Give Thumbs Up</a>
}

Here is what my Product model looks like:

public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }

public ICollection<ProductThumbsUp> ThumbsUps { get; set; }

解决方案

To make it efficient,

I would recommend getting all the products and instead of getting all the rows of thumbsUps for each product from DB, just get the like count for each product.

As you dont need each row of thumbsUp but just the count to display. Imagine the scenario for scale if you have a million likes on a product, suddenly you are pulling a million rows from the DB to your server - just to show a count.

Also i would make a second call to the database to get all the thumbsUps the current user has given (a list of product Ids he has liked). That will allow you to display to the user if they have liked it or now.

To add the acutal thumbs up to the product - you can send an ajax call which tells which productId he liked and mvc action can insert a thumbsUp row to the database.

这篇关于像处理并与使用AJAX在ASP.NET MVC的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆