2016-08-09 9 views
0

私はブログのウェブサイトを作ることによってASP.NET MVCとC#を学ぼうとしています。私は現在、投稿のあるページを持っており、個々の投稿の詳細ページ(urlのpostId(/ post/details/{id})を含む)から、私はコメントを作成するためのリンクをユーザに送ります。何らかの理由で、私の投稿IDは、私がCommentControllerのCreateクラスのパラメータとしてそれを添付するときには通っていません。これに関する助けがあれば大いに感謝します。前もって感謝します!投稿IDをURLから別のページに渡すにはどうすればよいですか?

RouteConfig.cs

public class RouteConfig 
{ 
    public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 
} 

ポスト詳細カミソリHTMLページ:

@model FantaC.Models.Post 

@{ 
    ViewBag.Title = "Details"; 
} 

<h2>Details</h2> 

<h2>Details</h2> 

<div class="row"> 
    <div class="col-md-8 whiteBorder scroll"> 
     <div class="postName"> 
      <h4>@Html.DisplayFor(model => model.PostName)</h4> 
      <h4>Written by: @Html.DisplayFor(model => model.UserName)</h4> 
      <img src="@Html.DisplayFor(model => model.PostImage)" /> 
     </div> 
     <div class="postContent"> 
      <p>@Html.DisplayFor(model => model.PostContent)</p> 
     </div> 
    </div> 
    <div class="col-md-4 whiteBorder scroll"> 
     <h4>Comments</h4> 
     <!--Comments attached to this post--> 
     @*@=Html.RenderAction("Create", "Comments", new { postId = Model.PostId });*@ 
     @*@Html.Partial("AddComment", Model.NewComment)*@ 
    </div> 
</div> 
<p> 
    @Html.ActionLink("Add a Comment", "Create", "Comment") | @*Redirects to create comment page*@ 
    @Html.ActionLink("Back to List", "Index") 
</p> 

コメントの作成/カミソリHTML:

@model FantaC.Models.Comment 

@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 


@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>Comment</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 

     <div class="form-group"> 
      @Html.LabelFor(model => model.CommentSubject, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.CommentSubject, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.CommentSubject, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.CommentContent, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.CommentContent, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.CommentContent, "", 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> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

CommentController.cs:

// GET: Comment/Create 
    public ActionResult Create() 
    { 
     return View(); 
    } 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(string id, [Bind(Include = "CommentSubject,CommentContent")] Comment model) // string id is null*** 
    { 
     if (ModelState.IsValid) 
     { 
      ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId()); 

      var commentId = (23817 + db.Comment.Count()).ToString().PadLeft(10, '0'); 

      var comment = new Comment 
      { 
       CommentId = commentId, 
       PostId = id, // id is null because it is null in parameter*** 
       UserName = user.UserName, 
       PostTime = DateTime.Now, 
       CommentSubject = model.CommentSubject, 
       CommentContent = model.CommentContent 
      }; 

      db.Comment.Add(comment); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(model); 
    } 
+1

'@ Html.ActionLink(" Add a Comment "、" Create " "Comment") 'これはどんな種類のIDも渡していません。それは '@ Html.ActionLink("コメントの追加 "、"作成 "、"コメント "、新しい{id = Model.PostId})'ではないでしょうか?また、CommentControllerに[HttpGet]バージョンのCreateを表示していないか、そのページのRazorを表示していません。これらの両方ともおそらくこれに影響を与えます。渡された投稿IDを処理して、作成のポストバックに再び使用できるようにする必要があります。 – ADyson

+0

ありがとうございます@アディソン。私はその情報を追加しますが、@ Html.ActionLink( "Add a Comment"、 "Create"、 "Comment"、new {id = Model.PostId})を試してみました。私が望むようなコメントページを作成してください。 –

+1

@ADysonのコメントが機能しない場合は、最後にヌルを追加してください: @ Html.ActionLink( "コメントを追加"、 "作成"、 "コメント"、新しい{id = Model.PostId}、null) –

答えて

1

@ADysonのコメントが動作しない場合は、ちょうど最後にはnullを追加します。

@Html.ActionLink("Add a Comment", "Create", "Comment", new { id= Model.PostId }, null) 

最後のパラメータがMAXLENGTHのように、HTML属性のためです。使用しているコンストラクタは(HtmlHelper、String、String、String、Object、Object)で、詳細はこちらをご覧ください:https://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink.aspx - Ricardo Pontual

関連する問題