在LINQ查询模型值使用列表 [英] Using list in a model value in LINQ query

查看:127
本文介绍了在LINQ查询模型值使用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net MVC开发的非常基本的阶段。所以有时候我用简单的LINQ查询工作奋斗。

I am at very basic stage of asp.net MVC development. So sometimes I struggle with simple LINQ queries to work.

场景 -

我有一个网页,有一些图片键,用户在该影像注释(就像从用户的含评论Facebook的一个职位)。

I have A page that has some Image and comment on that Image by users (Just Like a post on facebook containing comments from users).

所以我保存来自textarea的这些意见,并通过Ajax查询发送图片ID。

So I am saving those comments from the textarea and sending Image ID via Ajax query.

这是我的控制器的操作方法 -

保存评论 -

   [HttpPost]
        public void SaveComment(CardModel card) {
            CardCommentTable commenttable = new CardCommentTable();
            commenttable.CardComment = card.cardComment;
            commenttable.FKcardID = card.cardID;
            db.CardCommentTables.InsertOnSubmit(commenttable);
            db.SubmitChanges();
        }

此注释保存在 CardCommentTable 有在图像表的外键引用被保存。

This Comment is saved in CardCommentTable that has foreign key reference of Table in that Image is saved.

视图页面上渲染图像等领域 -

这使得查询图像等领域,使得它的图像后。像标题 dateofsubmit 等。

This query renders Image and other fields that make it An Image post. Like title, dateofsubmit, Like etc.

public ActionResult CardDetails(CardModel card) {

            var cardDetail = (from u in db.CardTables
                              where u.CardID == card.cardID
                              select new CardModel {
                                  cardID = u.CardID,
                                  cardHashCode = u.CardHashCode,
                                  cardDate = u.CardDate,
                                  cardFileName = u.CardFileName,
                                  cardFilePath = u.CardFilePath,
                                  cardTitle = u.CardTitle
                              }).ToList();
            return View(cardDetail);

        }

现在在 cardTable 我有一个名为 cardComment 在我想告诉所有的那些保存的评论多了一个列表 CardCommentTable

Now In cardTable I have one more column named cardComment in that I want to show all those saved comments from table CardCommentTable.

所以,我想 -

public ActionResult CardDetails(CardModel card) {
        var allsavedcomments= (from u in db.CardCommentTables
                           where u.FKcardID == card.cardID
                           select u).ToList();

        var cardDetail = (from u in db.CardTables
                          where u.CardID == card.cardID
                          select new CardModel {
                              cardID = u.CardID,
                              cardHashCode = u.CardHashCode,
                              cardDate = u.CardDate,
                              cardFileName = u.CardFileName,
                              cardFilePath = u.CardFilePath,
                              cardTitle = u.CardTitle,
                              cardComment = allsavedcomments // Trying to render all saved coments here.
                          }).ToList();
        return View(cardDetail);

    }

查看 -

@model IEnumerable<FunRanger.Models.CardModel>

@foreach (var item in Model) {
    <script type="text/javascript">
        $(function () {
            $('#save-comment').click(function () {
                var textareavalue = $('#textarea-comment').val();
                $.ajax({
                    url: '/Home/SaveComment/',
                    type: 'post',
                    data: '&cardComment=' + textareavalue + '&cardID=' + '@item.cardID',
                    success: function (data) {
                        $('#all-comments').append(data);


                    }

                });

            });

        });
    </script>
    using (Html.BeginForm()) {
    @Html.ValidationSummary(true)


        @if (Model != null) {


                <h2 class="header-wrapmain">
                    @item.cardTitle
                </h2>

                    @item.cardDate.ToShortDateString()


                        <img src="@item.cardFilePath" />

                                <a href="#" class="@item.cardHashCode" rel="tooltip" data-placement="bottom" title="Filter by @item.cardHashCode">
                                    #@item.cardHashCode</a>



        }
        else {
            <div class="alert alert-danger">
                No More items to preview
            </div>
        }


    }

    <textarea class="span12" rows="5" id="textarea-comment" style="resize: none" placeholder="Enter a comment..."></textarea>

        <a href="javascript:;" id="save-comment" class="btn-primary btn">Save comment</a>

        <ol>
            <li>
                @item.cardComment
            </li>
        </ol>

}

我怎样才能插入列表结果在这里的一列。

我如何证明这个上面的查询所有已保存的意见?

How do I show all saved comments with this above query?

感谢您的帮助。

推荐答案

我稍微装修你的code。与外键关系船。这将节省您的使用两个不同的查询到你的数据库(如你现在在做什么)。

I slightly renovated your code with Foreign key relations ship. This will save your from using two different queries to your database (like what you are doing now).

所以,如果你数据库模型是这样的 -

So if you Database Model looks like this -

然后,你应该有一个视图模型在以这种方式code -

Then you should have one viewmodel in your code in this way -

public class ImageViewModel
{
    public string ImageId { get; set; }
    public string ImageUrl { get; set; }
    public List<string> Comments { get; set; }
}

和控制器的动作将返回所有的结果应该是这样的 -

And your controller action which will return all the results should be like this -

 public class ListController : Controller
    {
        public ActionResult Index()
        {
            ImageViewModel model;
            using (SampleEntities entities = new SampleEntities())
            {
                model = (from p in entities.Images
                                     where p.ImageId == "1"
                                     select new ImageViewModel()
                                     {
                                         ImageId = p.ImageId,
                                         ImageUrl = p.ImageUrl,
                                         Comments = p.ImageComments.Select(pa => pa.Comment).ToList()
                                     }).FirstOrDefault();
            }

            return View(model);
        }
    }

最后,将显示所有图片搜索结果的看法 -

Finally the view which will display all the Image results -

@model MVC.Controllers.ImageViewModel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<div>
    <h4>ImageViewModel</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.ImageId)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.ImageId)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.ImageUrl)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.ImageUrl)
        </dd>
        <br/>
        @foreach (var item in Model.Comments)
        {
            @item <br/>
        }

    </dl>
</div>

输出会 -

这篇关于在LINQ查询模型值使用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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