2016-08-09 264 views
0

Ich versuche, ASP.NET MVC und C# durch die Erstellung einer Blog-Website zu lernen. Ich habe derzeit eine Seite mit Posts und von der Detailseite eines einzelnen Beitrags, der die Post-ID in der URL enthält (/ post/details/{id}), habe ich einen Link, der einen Benutzer zum Erstellen eines Kommentars sendet. Aus irgendeinem Grund kommt meine Post-ID nicht durch, wenn ich sie als Parameter für die Create-Klasse in meinem CommentController anhefte. Jede Hilfe dazu wäre sehr willkommen. Danke im Voraus!Wie gebe ich die Post-ID von der URL an eine andere Seite weiter?

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 } 
     ); 
    } 
} 

Veröffentlichungsdetails Razor HTML-Seite:

@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> 

Kommentar/Erstellen Razor 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 (" Kommentar hinzufügen "," Erstellen ", "Kommentar") 'Dies ist keine Art von ID übergeben. Sollte es nicht '@ Html.ActionLink sein (" Kommentar hinzufügen "," Erstellen "," Kommentar ", neu {id = Model.PostId})'? Außerdem haben Sie die [HttpGet] -Version von Create in Ihrem CommentController oder das Razor für diese Seite nicht angezeigt. Beides wird wahrscheinlich einen Einfluss darauf haben - sie müssen die übergebene Post-ID behandeln, damit sie im Postback erneut zu Create verwendet werden kann. – ADyson

+0

Danke @ADyson. Ich werde diese Informationen hinzufügen, aber ich habe versucht @ Html.ActionLink ("Kommentar hinzufügen", "Erstellen", "Kommentar", neue {ID = Model.PostId}) und es dauert mich zu erstellen eine Post-Seite anstelle der Erstellen Sie eine Kommentar-Seite wie ich es möchte. –

+1

Wenn der Kommentar von @ADyson nicht funktioniert, fügen Sie einfach eine Null am Ende: @ Html.ActionLink ("Kommentar hinzufügen", "Erstellen", "Kommentar", neue {ID = Model.PostId}, null) –

Antwort

1

wenn der Kommentar von @ADyson nicht funktioniert, nur eine Null am Ende hinzufügen:

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

Der letzte Parameter ist für HTML-Attribute, wie maxlength. Der Konstruktor, den Sie verwenden, ist (HtmlHelper, String, String, String, Object, Object), Sie können hier mehr sehen: https://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink.aspx - Ricardo Pontual