Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何拥有';详情';和';创建';同样的观点?MVC_C#_Sql Server_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 如何拥有';详情';和';创建';同样的观点?MVC

C# 如何拥有';详情';和';创建';同样的观点?MVC,c#,sql-server,asp.net-mvc,entity-framework,C#,Sql Server,Asp.net Mvc,Entity Framework,下面的代码(在控制器中)从上一页读取特定行数据(通过@Html.ActionLink(“细节”,“细节”,new{id=item.id})在视图中获取id) 到目前为止还不错,但现在我想允许访问者/用户留下评论,给出喜欢/不喜欢的东西等等。我想这是一个结合了细节/创建(换句话说,读取/插入)模板的视图?我必须在控制器中做什么才能使其工作 从现在开始,我不知道该做什么,因为我是MVC的新手 这就是我的数据库的样子:(从EF模型中选择(数据库优先)): 用户: public System.G

下面的代码(在控制器中)从上一页读取特定行数据(通过
@Html.ActionLink(“细节”,“细节”,new{id=item.id})
在视图中获取
id

到目前为止还不错,但现在我想允许访问者/用户留下评论,给出喜欢/不喜欢的东西等等。我想这是一个结合了细节/创建(换句话说,读取/插入)模板的视图?我必须在控制器中做什么才能使其工作

从现在开始,我不知道该做什么,因为我是MVC的新手

这就是我的数据库的样子:(从EF模型中选择(数据库优先)):

用户:

    public System.Guid Id { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
评论:(作者创建评论的地方)

CommentReview:(review从其他用户那里获得评论)

UserReview:(如果用户已经喜欢该评论,避免多次喜欢)

显示选定行详细信息的视图如下所示:

 <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Title)
        </dt>

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

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

        <dd>
            @Html.DisplayFor(model => model.Description)
        </dd>
@*  ... and so on.  Not sure how to add "create"-inputs for e.g. comments etc*@

@DisplayNameFor(model=>model.Title)
@DisplayFor(model=>model.Title)
@DisplayNameFor(model=>model.Description)
@DisplayFor(model=>model.Description)
@*  ... 等等不确定如何添加“创建”-输入,例如注释等*@

我正在使用
Session[“LoggedUserID”]
获取当前用户的
Id

首先稍微更改您的实体以配置它们之间的关系:

public class User
{
    [Key]
    public System.Guid Id { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }

    public virtual ICollection<CommentToReview> Comments { get; set; }
    public virtual ICollection<UserToReview> Reviews { get; set; }
}

public class Review
{
   [Key]
   public System.Guid Id { get; set; }
   public System.Guid CreatorUserId { get; set; }
   public string Title { get; set; }
   public string Description { get; set; }
   public System.DateTime CreatedDate { get; set; }
   public int UserRating { get; set; }
   public int LikeCount { get; set; }
   public int DislikeCount { get; set; }

   public virtual ICollection<CommentToReview> Comments { get; set; }
   public virtual ICollection<UserToReview> Users { get; set; }
}

public class CommentToReview
{
    [Key]
    public System.Guid Id { get; set; }
    [ForeignKey("User")]
    public System.Guid UserId { get; set; }
    public virtual User User { get; set; }
    [ForeignKey("Review")]
    public System.Guid ReviewId { get; set; }
    public virtual Review Review { get; set; }
    public string Comment { get; set; }
    public System.DateTime CreatedDate { get; set; } 
}

public class UserToReview
{
    [Key]
    public System.Guid Id { get; set; }
    [ForeignKey("User")]
    public System.Guid UserId { get; set; }
    public virtual User User { get; set; }
    [ForeignKey("Review")]
    public System.Guid ReviewId { get; set; }
    public virtual Review Review { get; set; }
    public bool HasLiked { get; set; }
}
然后,应在“视图/审阅”文件夹下创建以下部分视图:

_CreateCommentPartial.cshtml:用于在同一页的详细信息中创建注释

@using (Html.BeginForm("CreatePartial", "Reviews"))
{
@Html.AntiForgeryToken()   
<div class="form-horizontal">
    <h4>CommentToReview</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model=> model.ReviewId)

    <div class="form-group">
        @Html.LabelFor(model => model.Comment, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Comment, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Comment, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}


@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@使用(Html.BeginForm(“CreatePartial”、“Reviews”))
{
@Html.AntiForgeryToken()
评论视图

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @Html.HiddenFor(model=>model.ReviewId) @LabelFor(model=>model.Comment,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Comment,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Comment,“,new{@class=“text danger”}) } @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }
_CommentDetailPartial.cshtml:用于显示现有注释详细信息

@model MvcApp.Models.CommentToReview


<div class="panel panel-default">
<div class="panel-heading">@Model.User.Email</div>
<div class="panel-body">
    @Model.Comment
</div>
<div class="panel-footer">@Model.CreatedDate.ToString()
</div>
</div>
@model MvcApp.Models.commentoreview
@Model.User.Email
@模型.注释
@Model.CreatedDate.ToString()
最后,修改审阅的详细信息视图(Views/Reviews/Details.cshtml)

@model MvcApp.Models.Review
@{
ViewBag.Title=“详细信息”;
}
细节
复习

@DisplayNameFor(model=>model.Title) @DisplayFor(model=>model.Title) @DisplayNameFor(model=>model.Description) @DisplayFor(model=>model.Description) @DisplayNameFor(model=>model.CreatedDate) @DisplayFor(model=>model.CreatedDate) 评论
@foreach(Model.Comments中的var注释) { RenderPartial(“\u commentdetailsparial.cshtml”,comment); }
@{ var newComment=new MvcApp.Models.commentoreview{ReviewId=Model.Id}; Html.RenderPartial(“_CreateComment”,newComment); } @ActionLink(“编辑”,“编辑”,新的{id=Model.id})| @ActionLink(“返回列表”、“索引”)


请注意,为了简单起见,我在Review Controller和View下创建了所有PartialView和actions。另外,当你添加一条新的评论时,它会刷新页面,也许你最好使用ajax来获得更好的性能。

我明白了,你可以使用部分视图。只有一个问题,我如何共享数据,比如说一个带有部分视图的
Id
?例如,我想在
review
(view1)中显示特定行,我想
commentoreview
(view2)使用与
review
(view1)相同的
Id
。您可以使用该视图的模型,例如在partialview中使用
@Html.ActionLink(“commentoreview”,“Details”,new{Id=Model.Id})
若要发送该型号的特定
Id
,请创建一个新版本,并将您的代码和您尝试过的代码放在一起。也许这个示例对您有好处(不要忘记检查第2部分)
 <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Title)
        </dt>

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

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

        <dd>
            @Html.DisplayFor(model => model.Description)
        </dd>
@*  ... and so on.  Not sure how to add "create"-inputs for e.g. comments etc*@
public class User
{
    [Key]
    public System.Guid Id { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }

    public virtual ICollection<CommentToReview> Comments { get; set; }
    public virtual ICollection<UserToReview> Reviews { get; set; }
}

public class Review
{
   [Key]
   public System.Guid Id { get; set; }
   public System.Guid CreatorUserId { get; set; }
   public string Title { get; set; }
   public string Description { get; set; }
   public System.DateTime CreatedDate { get; set; }
   public int UserRating { get; set; }
   public int LikeCount { get; set; }
   public int DislikeCount { get; set; }

   public virtual ICollection<CommentToReview> Comments { get; set; }
   public virtual ICollection<UserToReview> Users { get; set; }
}

public class CommentToReview
{
    [Key]
    public System.Guid Id { get; set; }
    [ForeignKey("User")]
    public System.Guid UserId { get; set; }
    public virtual User User { get; set; }
    [ForeignKey("Review")]
    public System.Guid ReviewId { get; set; }
    public virtual Review Review { get; set; }
    public string Comment { get; set; }
    public System.DateTime CreatedDate { get; set; } 
}

public class UserToReview
{
    [Key]
    public System.Guid Id { get; set; }
    [ForeignKey("User")]
    public System.Guid UserId { get; set; }
    public virtual User User { get; set; }
    [ForeignKey("Review")]
    public System.Guid ReviewId { get; set; }
    public virtual Review Review { get; set; }
    public bool HasLiked { get; set; }
}
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreateComment([Bind(Include = "Id,UserId,ReviewId,Comment,CreatedDate")] CommentToReview commentToReview)
    {
        if (ModelState.IsValid)
        {
            commentToReview.Id = Guid.NewGuid();
            commentToReview.UserId = Session["LoggedUserID"].ToString();
            commentToReview.CreatedDate = DateTime.Now;
            db.CommentToReviews.Add(commentToReview);
            db.SaveChanges();
            return RedirectToAction("Details", "Reviews", new { id = commentToReview.ReviewId });
        }

        return RedirectToAction("Details", "Reviews", new { id = commentToReview.ReviewId });
    }
@using (Html.BeginForm("CreatePartial", "Reviews"))
{
@Html.AntiForgeryToken()   
<div class="form-horizontal">
    <h4>CommentToReview</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model=> model.ReviewId)

    <div class="form-group">
        @Html.LabelFor(model => model.Comment, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Comment, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Comment, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}


@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@model MvcApp.Models.CommentToReview


<div class="panel panel-default">
<div class="panel-heading">@Model.User.Email</div>
<div class="panel-body">
    @Model.Comment
</div>
<div class="panel-footer">@Model.CreatedDate.ToString()
</div>
</div>
@model MvcApp.Models.Review

@{
   ViewBag.Title = "Details";
}

<h2>Details</h2>

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

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

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

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

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

    <dd>
        @Html.DisplayFor(model => model.CreatedDate)
    </dd>    
</dl>
<h6>Comments</h6>
<hr />
@foreach(var comment in Model.Comments)
{
    Html.RenderPartial("_CommentDetailsPartial.cshtml", comment);
}

<br />
@{
    var newComment = new MvcApp.Models.CommentToReview { ReviewId = Model.Id         };

    Html.RenderPartial("_CreateComment", newComment);
}
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
    @Html.ActionLink("Back to List", "Index")
</p>